Ejemplo n.º 1
0
 /// <summary>
 /// 接收消息
 /// </summary>
 private void thdRevMethod()
 {
     try
     {
         while (true)
         {
             byte[] resByte = new byte[1024];
             int    resInt  = socketClient.Receive(resByte);
             if (resInt > 0)
             {
                 RevMsg.Invoke(Encoding.Default.GetString(resByte, 0, resInt));
                 Console.WriteLine(Encoding.Default.GetString(resByte, 0, resInt));
             }
         }
     }
     catch (SocketException sex)
     {
         if (sex.SocketErrorCode == SocketError.ConnectionReset)
         {
             Console.WriteLine("服务端断开!5s后重连!");
             reconnectTimer.Start();
             heartbeatTimer.Stop();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("程序出现异常:" + ex);
         heartbeatTimer.Stop();
     }
 }
 private void SetMsg(string msg)
 {
     RevMsg.Invoke(msg);
 }
Ejemplo n.º 3
0
        private void thdRevVarMethod()
        {//socketClient
         //byte[] resByte = new byte[1024];
         //int resInt = socketClient.Receive(resByte);
         //if (resInt > 0)
         //{
         //    RevMsg.Invoke(Encoding.Default.GetString(resByte, 0, resInt));
         //    Console.WriteLine(Encoding.Default.GetString(resByte, 0, resInt));
         //}

            int total = 0;
            int recv;

            byte[] datasize = new byte[4];


            while (true)
            {
                try
                {
                    GC.Collect();
                    recv = socketClient.Receive(datasize, 0, 4, SocketFlags.None);
                    int    size     = BitConverter.ToInt32(datasize, 0);
                    int    dataleft = size;
                    byte[] data     = new byte[size];
                    if (data.Length == 0)
                    {
                        continue;
                    }
                    var cts = new CancellationTokenSource();
                    cts.Token.Register(() =>
                    {
                        size     = 0;
                        dataleft = 0;

                        recv  = 0;
                        total = 0;
                        data  = null;

                        Console.WriteLine("线程中止");
                    });

                    cts.CancelAfter(50);


                    while (total < size)
                    {
                        recv = socketClient.Receive(data, total, dataleft, SocketFlags.None);
                        Console.WriteLine(string.Format("total : {0} , dataleft : {1}\n", total.ToString(), dataleft.ToString()));
                        if (recv == 0)   //接收到空字节时
                        {
                            data = null;
                            break;
                        }

                        total    += recv;
                        dataleft -= recv;
                    }
                    Console.WriteLine(Encoding.Default.GetString(data, 0, size));
                    RevMsg.Invoke(Encoding.Default.GetString(data, 0, size));
                }
                catch (SocketException sex)
                {
                    if (sex.SocketErrorCode == SocketError.ConnectionReset)
                    {
                        Console.WriteLine("服务端断开!5s后重连!");
                        reconnectTimer.Start();
                        heartbeatTimer.Stop();
                    }
                    if (sex.SocketErrorCode == SocketError.ConnectionAborted)
                    {
                        Console.WriteLine("服务端断开!5s后重连!");
                        reconnectTimer.Start();
                        heartbeatTimer.Stop();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("程序出现异常:" + ex);
                    heartbeatTimer.Stop();
                }
            }
        }