/// <summary> /// 处理客户端连接请求,成功后把客户端加入到clientPool /// </summary> /// <param name="result">Result.</param> private void Accept(IAsyncResult result) { Socket server = result.AsyncState as Socket; Socket client = server.EndAccept (result); try { //处理下一个客户端连接 server.BeginAccept (new AsyncCallback (Accept), server); byte[] buffer = new byte[65536]; //接收客户端消息 client.BeginReceive (buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback (Recieve), client); ClientInfo info = new ClientInfo (); info.Id = client.RemoteEndPoint; info.handle = client.Handle; info.buffer = buffer; //把客户端存入clientPool this.clientPool.Add (client, info); Console.WriteLine (string.Format ("Client {0} connected", client.RemoteEndPoint)); } catch (Exception ex) { Console.WriteLine ("Error :\r\n\t" + ex.ToString ()); } }
/// <summary> /// 处理客户端连接请求,成功后把客户端加入到clientPool /// </summary> /// <param name="result">Result.</param> private void Accept(IAsyncResult result) { Socket server = result.AsyncState as Socket; Socket client = server.EndAccept(result); try { //处理下一个客户端连接 server.BeginAccept(new AsyncCallback(Accept), server); byte[] buffer = new byte[65536]; //接收客户端消息 client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Recieve), client); ClientInfo info = new ClientInfo(); info.Id = client.RemoteEndPoint; info.handle = client.Handle; info.buffer = buffer; //把客户端存入clientPool clientPool.Add(client, info); } catch //(Exception ex) { } }