C#实现一个可接收信息的服务器端?简易的
1个回答
展开全部
private void button_start_Click(object sender, EventArgs e)
{
Thread a = new Thread(ser1);
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(maskedTextBox2.Text));
Socket ser = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ser.Bind(ip);
ser.Listen(20);
a.IsBackground = true;
a.Start(ser);
}
private void ser1(object o)//用户端登陆监听
{
Socket listen, lis = (Socket)o;
while (true)
{
try
{
listen = lis.Accept();
Thread a = new Thread(receive);
a.IsBackground = true;
a.Start(listen);
}
catch { }
}
}
private void receive(object o)//接受处理用户登陆消息
{
string ss = "";//保存接受到的消息
Socket newsocket = (Socket)o;
byte[] bb = new byte[1024];
try
{
bb = new byte[1024];
newsocket.Receive(bb, bb.Length, 0);
}
catch { return; }
ss = System.Text.Encoding.BigEndianUnicode.GetString(bb);
//后面就可以处理收到的信息了
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询