资源描述:
《KeepAlive异常检测代码.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、public AsyncSocket(Socket sock,int index) { this.index = index; this.sock = sock; this.SetXinTiao(this.sock); Socket obj_Socket = sock; StateObject obj_SocketState = new StateObject(); obj_SocketState.workSock
2、et = obj_Socket; obj_Socket.BeginReceive(obj_SocketState.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), obj_SocketState); } //设置心跳 private void SetXinTiao(Socket tmpsock) { byte[] inValue =
3、 new byte[] { 1, 0, 0, 0, 0x20, 0x4e, 0, 0, 0xd0, 0x07, 0, 0 };// 首次探测时间20 秒, 间隔侦测时间2 秒 tmpsock.IOControl(IOControlCode.KeepAliveValues, inValue, null); } private void ReceiveCallback(IAsyncResult ar) { try { Sta
4、teObject obj_SocketState = (StateObject)ar.AsyncState; Socket obj_Socket = obj_SocketState.workSocket; int BytesRead = obj_Socket.EndReceive(ar); if (BytesRead > 0) { byte[] tmp = new byte[BytesRead];
5、 Array.ConstrainedCopy(obj_SocketState.buffer, 0, tmp, 0, BytesRead); if (socketDataArrival!=null) { socketDataArrival(this.index, tmp); } } else
6、 { if (this.sock.Connected) { if (socketDisconnected!=null) { socketDisconnected(index); } } }
7、obj_Socket.BeginReceive(obj_SocketState.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), obj_SocketState); } catch (Exception ex) { if (socketDisconnected!=null) {
8、 socketDisconnected(ind