void AcceptCb(IAsyncResult ar) { try { Socket socket = listenfd.EndAccept(ar); int index = NewIndex(); if (index < 0) { Console.WriteLine("[ServNet.AcceptCb] conns is full."); } else { Conn conn = conns[index]; conn.Init(socket); conn.socket.BeginReceive( conn.readBuff, conn.buffCount, conn.BufferRemain(), SocketFlags.None, ReceiveCb, conn); } listenfd.BeginAccept( AcceptCb, null); } catch (Exception ex) { Console.WriteLine("[ServNet.AcceptCb] Accept socket fail. " + ex.Message); } }
//与客户端建立链接 private void AcceptCb(IAsyncResult ar) { try { Socket clientSocket = socket.EndAccept(ar); int index = NewIndex(); if (index < 0) { clientSocket.Close(); Console.WriteLine("[警告]连接池已满"); } else { Conn conn = conns[index]; conn.Init(clientSocket); Console.WriteLine("客户端连接[" + conn.GetAdress() + "]"); conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); } socket.BeginAccept(AcceptCb, null); } catch (Exception e) { Console.WriteLine("AcceptCb失败:" + e.Message); } }
private void AcceptCb(IAsyncResult ar) { try { Socket socket = listenfd.EndAccept(ar); int index = NewIndex(); if (index < 0) { socket.Close(); Console.Write("[警告]连接已满"); } else { Conn conn = conns[index]; conn.Init(socket); string adr = conn.GetAddr(); Console.WriteLine("客户端连接[" + adr + "]conn池ID:" + index); conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BuffRemain(), SocketFlags.None, ReceiveCb, conn); listenfd.BeginAccept(AcceptCb, null); } } catch (Exception e) { Console.WriteLine("AcceptCb失败:" + e.Message); } }