Exemplo n.º 1
0
 /// 〈summary〉
 /// 客户通道发送数据
 /// 〈/summary〉
 /// 〈param name="datagram"〉要发送的数据〈/param〉
 public void Send(byte[] DataToSend)
 {
     if (_isClose)
     {
         OnError(this, "通道已关闭");
         return;
     }
     if (HandlerType == (int)_HandlerType.LHandler)           //如果不是侦听通道时,则出错.
     {
         OnError(this, "服务器端侦听通道不能使用Send方法进行通信!");
         return;
     }
     try
     {
         if (ClientSocket.Connected)
         {
             byte[]       sendBytes = agree.SendDataConverse(DataToSend);
             IAsyncResult iar;
             iar = ClientSocket.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, null, null);
             bool b = iar.AsyncWaitHandle.WaitOne();
             if (b)
             {
                 SendDataEnd(iar);
             }
         }
         else
         {
             OnError(this, "远程主机未连接,发送数据失败!");
         }
     }
     catch (Exception e)
     {
         OnError(this, e.Message);
     }
 }