using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketServer
{
public partial class ServerForm : Form
{
//建立监听器
TcpListener Listener = null;
public ServerForm()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void btnListener_Click(object sender, EventArgs e)
{
//监听对象
Listener = new TcpListener(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));
Listener.Start(50);
//创建新线程执行监听
ThreadStart ts = new ThreadStart(Listeners);
Thread th = new Thread(ts);
//开启线程
th.Start();
}
public void Listeners()
{
//循环监听
while (true)
{
Socket sk = Listener.AcceptSocket();
txtMessage.Text += "连接成功";
byte[] bytes = new byte[1024];
sk.Receive(bytes);
txtMessage.Text += Encoding.Default.GetString(bytes);
}
}
private void ServerForm_Load(object sender, EventArgs e)
{
}
}
}