Ejemplo n.º 1
0
 /// <summary>
 /// 發送數據報文
 /// </summary>
 /// <param name="datagram"></param>
 public virtual void Send(string datagram)
 {
     if (datagram.Length == 0)
     {
         return;
     }
     if (!_isConnected)
     {
         throw (new ApplicationException("沒有連接服務器,不能發送數據"));
     }
     //獲得報文的編碼字節
     byte[] data = _coder.GetEncodingBytes(datagram);
     if (HeadLength)
     {
         int    arrayLength     = IPAddress.HostToNetworkOrder(data.Length);
         byte[] lengthByteArray = System.BitConverter.GetBytes(arrayLength);
         byte[] Bytes           = new byte[data.Length + 4];
         Array.Copy(lengthByteArray, 0, Bytes, 0, 4);
         Array.Copy(data, 0, Bytes, 4, data.Length);
         _session.ClientSocket.BeginSend(Bytes, 0, Bytes.Length, SocketFlags.None, new AsyncCallback(SendDataEnd), _session.ClientSocket);
     }
     else
     {
         _session.ClientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendDataEnd), _session.ClientSocket);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="recvDataClient">接收数据的客户端会话</param>
 /// <param name="datagram">数据报文</param>
 public virtual void Send(Session recvDataClient, string datagram)
 {
     //获得数据编码
     byte[] data = _coder.GetEncodingBytes(datagram);
     if (HeadLength)
     {
         int    arrayLength     = IPAddress.HostToNetworkOrder(data.Length);
         byte[] lengthByteArray = System.BitConverter.GetBytes(arrayLength);
         byte[] Bytes           = new byte[data.Length + 4];
         Array.Copy(lengthByteArray, 0, Bytes, 0, 4);
         Array.Copy(data, 0, Bytes, 4, data.Length);
         recvDataClient.ClientSocket.BeginSend(Bytes, 0, Bytes.Length, SocketFlags.None,
                                               new AsyncCallback(SendDataEnd), recvDataClient.ClientSocket);
     }
     else
     {
         recvDataClient.ClientSocket.BeginSend(data, 0, data.Length, SocketFlags.None,
                                               new AsyncCallback(SendDataEnd), recvDataClient.ClientSocket);
     }
 }