private void ServerAcceptCallback(IAsyncResult InIAsyncResult) { try { MyTcpClient = (TcpClient)InIAsyncResult.AsyncState; MyTcpClient.EndConnect(InIAsyncResult); if (MyTcpClient.Connected) { ConnectID = 0;//成功连接标志为0 ReceiveIDFlag = 1; //Interlocked.Decrement(ConnectID); MyNetworkStream = MyTcpClient.GetStream(); ReadWriteSocketChannel MyReadWriteSocketChannel = new ReadWriteSocketChannel(MyNetworkStream, MyTcpClient.ReceiveBufferSize); MyNetworkStream.BeginRead(MyReadWriteSocketChannel.RecieveMessageBytes, 0, MyReadWriteSocketChannel.RecieveMessageBytes.Length, AsynchReadDataCallback, MyReadWriteSocketChannel); } else { ConnectID = 1; } } catch (Exception ExceptionInfor) { ConnectID = 2; } MyEventWaitDone.Set(); }
//===========Receive================================================================= protected virtual void AsynchReadDataCallback(IAsyncResult InIAsyncResult) { ReadWriteSocketChannel MyReadWriteSocketChannel = (ReadWriteSocketChannel)InIAsyncResult.AsyncState; try { int RecieveCount = MyReadWriteSocketChannel.netStream.EndRead(InIAsyncResult); if (RecieveCount == 0) { ReceiveIDFlag = 1; ConnectID = 1;//对方主机关闭! //--1.------------------------------ //OnRemoteServerColseEvent(new EventArgs()); //--2.-------------------------------- if (MyRemoteServerColseCallBack != null) { MyRemoteServerColseCallBack(); //引发关机事件! } //--3..-------------------------------- //if (RemoteServerColseCallBackEvent != null) //{ //MessageBox.Show("COM Client Call!!!...ServerColse "); //RemoteServerColseCallBackEvent.Invoke(); //} } else { //ReceiveDataStr.Append(Encoding.UTF8.GetString(MyReadObject.RecieveMessageBytes, 0, count)); ReceiveMessageBuffers = MyReadWriteSocketChannel.RecieveMessageBytes; RecieveMessageParser(ReceiveMessageBuffers, RecieveCount); ReceiveIDFlag = 0; if (ThreadIsExit == false) { MyReadWriteSocketChannel = new ReadWriteSocketChannel(MyNetworkStream, MyTcpClient.ReceiveBufferSize); MyNetworkStream.BeginRead(MyReadWriteSocketChannel.RecieveMessageBytes, 0, MyReadWriteSocketChannel.RecieveMessageBytes.Length, AsynchReadDataCallback, MyReadWriteSocketChannel);//继续接收数据! } } } catch (Exception ExceptionInfor) { bool IsConnected = MyTcpClient.Client.Connected; if (IsConnected) { if (ExceptionInfor.Message.IndexOf("无法从传输连接中读取数据") > -1) { ReceiveIDFlag = 2; ConnectID = 1;//对方主机关闭! //--1.--------------------------------------------------------------------- //OnRemoteServerColseEvent(new EventArgs()); //--2.----------------------------------------------------------------------- if (MyRemoteServerColseCallBack != null) { MyRemoteServerColseCallBack(); } //--3..----------------------------------------------------------------------- //if (RemoteServerColseCallBackEvent != null) //{ //MessageBox.Show("COM Client Call!!!...ServerColse "); //RemoteServerColseCallBackEvent.Invoke(); //} } else { ReceiveIDFlag = 3;//待验证 } } else { ConnectID = 1;//对方主机关闭! ReceiveIDFlag = 4; //OnRemoteServerColseEvent(new EventArgs()); if (MyRemoteServerColseCallBack != null) { MyRemoteServerColseCallBack(); } //if (RemoteServerColseCallBackEvent != null) //{ //MessageBox.Show("COM Client Call!!!...ServerColse "); //RemoteServerColseCallBackEvent.Invoke(); //} } } finally { /* * if (ThreadIsExit == false) * { * MyReadObject = new ReadObject(MyNetworkStream, MyTcpClient.ReceiveBufferSize); * MyNetworkStream.BeginRead(MyReadObject.RecieveMessageBytes, 0, MyReadObject.RecieveMessageBytes.Length, AsynchReadDataCallback, MyReadObject);//继续接收数据! * } */ // MyEventWaitDone.Set();//结束读取数据! } }