Пример #1
0
 /// <summary>
 /// 接收消息
 /// </summary>
 public void RecieveMsg()
 {
     while (!isThreadEnd)
     {
         try
         {
             //接收字节数据并返回长度
             int n = _clientSocket.Receive(_buffer);
             //Debug.Log(n);
             MyParser.ConstructPacket(_buffer, n);
             byte[] temp = null;
             while ((temp = MyParser.GetSingleData()) != null)
             {
                 //将消息转化为字符串
                 string msg = MyParser.DecoderData(temp);
                 //显示消息
                 //Console.WriteLine(msg);
                 Debug.Log(msg);
                 lock (MessageBox)
                 {
                     MessageBox.Enqueue(msg);
                 }
                 //txtHandle(msg);
             }
         }
         catch (Exception e)
         {
             //Console.WriteLine(e.ToString());
             Debug.Log(e.ToString());
             break;//断开连接
         }
     }
 }
Пример #2
0
 /// <summary>
 /// 发送消息
 /// </summary>
 /// <param name="msg">消息</param>
 public void SendMsg(string msg)
 {
     try
     {
         //_clientSocket.Send(MyParser.EncoderData(msg));
         byte[] buffer = MyParser.EncoderData(msg);
         _clientSocket.BeginSend(buffer, 0, buffer.Length,
                                 SocketFlags.None,
                                 (ia) => { ((Socket)ia.AsyncState).EndSend(ia); },
                                 _clientSocket
                                 );
     }
     catch (Exception e)
     {
         //Console.WriteLine(e.ToString());
         Debug.Log(e.ToString());
         StartConnect();
     }
 }