|
做服务端
主要代码如下:
public SocketServer(EndPoint endPoint)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(endPoint);
}
public void Start()
{
connectDone = new ManualResetEvent(false);
connectThread = new Thread(new ThreadStart(ConnectService));
connectThread.IsBackground = true;
socket.Listen(1024);
connectThread.Start();
}
protected void ConnectService()
{
while (true)
{
connectDone.Reset();
socket.BeginAccept(new AsyncCallback(ConnectInit), socket);
connectDone.WaitOne();
}
}
protected void ConnectInit(IAsyncResult ar)
{
Socket clientSocket = ((Socket) ar.AsyncState).EndAccept(ar);
connectDone.Set();
if (OnConnect != null)
{
NetworkClient client = new NetworkClient(clientSocket);
OnConnect(client);
}
}
当客户端有3个以上的用户同时连接时,3个以后的连接都连接不上。望大家帮我看看。 |
|