C#中怎么在一个端口连接多个客户端?
private void StartListen()
{
myTcpListener = new TcpListener(8444);
try
{
myTcpListener.Start();
Socket mySocket = myTcpListener.AcceptSocket();
while (true)
{
if (mySocket.Connected)
{ myNetworkStream = new NetworkStream(mySocket);
byte[] data = new byte[1024];
myNetworkStream.Read(data, 0, data.Length);
}
}
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message);
}
}
private void Send(string sendmesg)
{
byte[] data = Encoding.ASCII.GetBytes(sendmesg);
myNetworkStream.Write(data, 0, data.Length);
}
private void onChange(string msg2)
{
.....
}
}
当然我现在写的这个监听方法,只能连接一个,能有什么方法能这个端口同时连接多个客户端吗?望大家指导。。
------解决方案--------------------
创建一个线程,后台循环接收