Esempio n. 1
0
 /// <summary>
 /// 心跳定时器
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 /// <param name="_socket">对应的socket客户端实例</param>
 private void heartbeatTimerIsUp(object sender, ElapsedEventArgs e, Socket _socket)
 {
     //如果客户端的状态是连接着的,则发送心跳消息;否则从客户端列表中移除该客户端
     try
     {
         if (_socket.Connected && IsClose != true)
         {
             byte[] sendMsg = new byte[2];
             if (sLock == false)
             {
                 _socket.Send(sendMsg);
             }
         }
         else
         {
             if (socketTimerDic.ContainsKey(_socket))
             {
                 socketTimerDic.Remove(_socket);
                 CustomMsg msg = new CustomMsg(1, _socket.RemoteEndPoint.ToString(), "断开成功!");
                 RevMsg.Invoke(msg);
             }
         }
     }
     catch (System.Net.Sockets.SocketException)
     {
         socketTimerDic.Remove(_socket);
         CustomMsg msg = new CustomMsg(1, _socket.RemoteEndPoint.ToString(), "断开成功!");
         RevMsg.Invoke(msg);
         return;
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 开始监听
        /// </summary>
        private void ListenSocket()
        {
            try
            {
                while (true)
                {
                    if (IsClose)
                    {
                        break;
                    }
                    Socket    _socket = socketServer.Accept();
                    CustomMsg msg     = new CustomMsg(0, _socket.RemoteEndPoint.ToString(), "连接成功!");
                    RevMsg.Invoke(msg);
                    Console.WriteLine("新客户端连接成功");
                    //开始心跳
                    System.Timers.Timer heartbeatTimer = new System.Timers.Timer();
                    heartbeatTimer.Interval = 1500;
                    heartbeatTimer.Elapsed += new System.Timers.ElapsedEventHandler((s, e) => heartbeatTimerIsUp(s, e, _socket));
                    heartbeatTimer.Start();



                    socketTimerDic.Add(_socket, null);

                    Thread thdReceive = new Thread(new ParameterizedThreadStart(thdRevMethod));
                    thdReceive.Start(_socket);

                    ClientList.Add(_socket);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("程序出现异常:" + ex);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="obj"></param>
        private void thdRevMethod(object obj)
        {
            Socket _socket = obj as Socket;

            while (true)
            {
                try
                {
                    if (IsClose)
                    {
                        return;
                    }
                    byte[] resByte = new byte[1024];
                    int    resInt  = _socket.Receive(resByte);
                    if (resInt > 0)
                    {
                        string res = Encoding.Default.GetString(resByte, 0, resInt);
                        if (RevMsg != null)
                        {
                            CustomMsg msg = new CustomMsg(3, _socket.RemoteEndPoint.ToString(), res);
                            RevMsg.Invoke(msg);
                            Console.WriteLine(msg.msg);
                        }
                    }
                }
                catch (SocketException sex)
                {
                    if (sex.SocketErrorCode == SocketError.ConnectionReset)
                    {
                        //当客户端断开连接,从列表中移除该客户端
                        if (socketTimerDic.ContainsKey(_socket))
                        {
                            socketTimerDic.Remove(_socket);

                            CustomMsg msg = new CustomMsg(1, _socket.RemoteEndPoint.ToString(), "断开成功!");
                            RevMsg.Invoke(msg);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("程序出现异常:" + ex);
                    continue;
                }
            }
        }