예제 #1
0
 private void ReadCallBack(IAsyncResult iar)
 {
     try
     {
         DataRead dataRead = (DataRead)iar.AsyncState;
         int      recv     = dataRead.ns.EndRead(iar);
         if (recv != 0)
         {
             byte[] receBytes = new byte[recv];
             Array.Copy(dataRead.msg, 0, receBytes, 0, recv);
             if (isExit == false)
             {
                 dataRead = new DataRead(ns, client.ReceiveBufferSize);
                 ns.BeginRead(dataRead.msg, 0, dataRead.msg.Length, ReadCallBack, dataRead);
             }
             //这里发送数据事件
             PortDataReceived(new ReceBytesEventArge(receBytes));
         }
         else
         {
             //这里表示掉线了
             portStatus = PortStatus.Closed;
             ConnNotify(new ConnStatusChangedEventArgs(DateTime.Now, false, portStatus));
         }
     }
     catch (Exception ex)
     {
         portStatus = PortStatus.Error;
         ConnNotify(new ConnStatusChangedEventArgs(DateTime.Now, false, portStatus));
     }
 }
예제 #2
0
 private void RequestCallBack(IAsyncResult iar)
 {
     try
     {
         portStatus = PortStatus.Connecting;
         client     = (TcpClient)iar.AsyncState;
         client.EndConnect(iar);
         portStatus = PortStatus.Connected;
         ConnNotify(new ConnStatusChangedEventArgs(DateTime.Now, true, portStatus));
         ns = client.GetStream();
         DataRead dataRead = new DataRead(ns, client.ReceiveBufferSize);
         ns.BeginRead(dataRead.msg, 0, dataRead.msg.Length, ReadCallBack, dataRead);
     }
     catch (Exception ex)
     {
         portStatus = PortStatus.Error;
         ConnNotify(new ConnStatusChangedEventArgs(DateTime.Now, false, portStatus));
     }
 }