/// <summary> /// 打开服务器连接 /// </summary> /// <param name="prot">端口号</param> /// <param name="maxcount">最大连接数</param> public void Start(int prot, int maxcount) { try { serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); acceptSemaphore = new Semaphore(maxcount, maxcount); clientPeerPool = new ClientpeerPool(maxcount); Clientpeer temClientpeer = null; for (int i = 0; i < maxcount; i++) { temClientpeer = new Clientpeer(); temClientpeer.receveargs.Completed += Receive_Completed; temClientpeer.receivecompleted = receiveCompleted; clientPeerPool.Enqueue(temClientpeer); } serverSocket.Bind(new IPEndPoint(IPAddress.Any, prot)); serverSocket.Listen(10); Console.WriteLine("服务器启动成功!!!"); startAccept(null); } catch (Exception e) { Console.WriteLine(e.ToString()); throw new Exception("打开服务器失败!!!!"); } }
/// <summary> /// 处理接收的请求 /// </summary> /// <param name="e"></param> private void ProcessReceive(SocketAsyncEventArgs e) { Clientpeer client = e.UserToken as Clientpeer; //异步套接字结果成功且传输字节大于0 if (client.receveargs.SocketError == SocketError.Success && client.receveargs.BytesTransferred > 0) { byte[] packet = new byte[client.receveargs.BytesTransferred]; Buffer.BlockCopy(client.receveargs.Buffer, 0, packet, 0, client.receveargs.BytesTransferred); //客户端自身处理这个数据包,自身解析 client.StartReceive(packet); } //断开连接 如果没有传输的字节数 就代表断开连接了 else if (client.receveargs.BytesTransferred == 0) { if (client.receveargs.SocketError == SocketError.Success) { //客户端主动断开 //TODO } else { //由于网络异常 导致 被动断开连接 //TODO } } }
private void StartReceive(Clientpeer client) { try { SocketAsyncEventArgs clientrecevearges = client.receveargs; bool result = client.ClientSocket.ReceiveAsync(clientrecevearges); if (!result) { ProcessReceive(clientrecevearges); } } catch (Exception e) { Console.WriteLine(e.Message); } }
private void processResult(SocketAsyncEventArgs e) { //Socket clientSocket = e.AcceptSocket; //保存客户端socket Clientpeer client = clientPeerPool.Dequeue(); client.ClientSocket = e.AcceptSocket; //开始接收数据 StartReceive(client); //递归调用 e.AcceptSocket = null; startAccept(e); }
public void Enqueue(Clientpeer client) { clientpeerspool.Enqueue(client); }
/// <summary> /// 接收完一条消息时的回调 /// </summary> /// <param name="client"></param> /// <param name="value"></param> private void receiveCompleted(Clientpeer client, SocketMsg value) { //给应用层 让其使用 //TODO }