异步Socket 心跳包问题
看了你以下的回答,请问这样设置后如果用户异常退出或者网络断线,在代码里怎么体现出来呢?是在catch里获取么?我的客户端退出后并没有catch到消息,反而是在EndRec...
看了你以下的回答,请问这样设置后如果用户异常退出或者网络断线,在代码里怎么体现出来呢?是在catch里获取么?我的客户端退出后并没有catch到消息,反而是在EndReceive(ar); 时捕获到异常,异常消息为 “远程主机强迫关闭了一个现有的连接”,请问在哪里处理客户端退出呢? try { //将事件状态设为终止状态,允许一个或多个等待线程继续 allDone.Set(); Socket listener = (Socket)ar.AsyncState; Socket client = listener.EndAccept(ar); client.IOControl(IOControlCode.KeepAliveValues, Common.KeepAlive(0, 2000, 4000), null); SessionInfo ClientInfo = new SessionInfo(); ClientInfo.ClientSocket = client; if (client != null && client.Connected) { client.BeginReceive(ClientInfo.Buffer, 0, ClientInfo.BufferSize, 0, new AsyncCallback(readCallback), ClientInfo); // 开始从客户端套接字异步读取数据。 } } catch (Exception ex) { WriteData.WriteLine("1000: " + ex.ToString()); } const int KEEPALIVESIZE = 12;static byte[] KeepAlive(int onOff, int keepAliveTime, int keepAliveInterval) { byte[] buffer = new byte[KEEPALIVESIZE]; BitConverter.GetBytes(onOff).CopyTo(buffer, 0); BitConverter.GetBytes(keepAliveTime).CopyTo(buffer, 4); BitConverter.GetBytes(keepAliveInterval).CopyTo(buffer, 8); return buffer; }设置的地方:Socket clientSocket = Socket.EndAccept(ar); //在 EndAccept 以后clientSocket.IOControl(IOControlCode.KeepAliveValues, KeepAlive(0, 30000, 5000), null);
展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询