日期:2014-05-18  浏览次数:20855 次

socket 如何设置心跳判断连接
老大有个思路就是要在客户端长时间未操作连接的时候定时发送心跳脉冲数据包测试是否连接 
这个如何实现呀?求具体思路或者少许代码,拜谢拜谢

------解决方案--------------------
在定时器里隔段时间就发送几个字节的数据。如果3次没有返回则断开
------解决方案--------------------
客户端:
30秒发送一个心跳包到服务器

服务器:
接收到心跳包,更新LastHeartbeatTime
并且有一个线程,一分钟扫描一次,如果LastHeartbeatTime超过一分钟没更新的视为下线
 


------解决方案--------------------
C# code

模拟心跳的机制
使用直接调用函数模拟心跳,没有涉及到socket
写得不好,不要太挑剔

using System;
using System.Collections.Generic;
using System.Threading;

namespace ConsoleApplication1
{
    // 客户端离线委托
    public delegate void ClientOfflineHandler(ClientInfo client);

    // 客户端上线委托
    public delegate void ClientOnlineHandler(ClientInfo client);

    public class Program
    {
        /// <summary>
        /// 客户端离线提示
        /// </summary>
        /// <param name="clientInfo"></param>
        private static void ClientOffline(ClientInfo clientInfo)
        {
            Console.WriteLine(String.Format("客户端{0}离线,离线时间:\t{1}", clientInfo.ClientID, clientInfo.LastHeartbeatTime));
        }

        /// <summary>
        /// 客户端上线提示
        /// </summary>
        /// <param name="clientInfo"></param>
        private static void ClientOnline(ClientInfo clientInfo)
        {
            Console.WriteLine(String.Format("客户端{0}上线,上线时间:\t{1}", clientInfo.ClientID, clientInfo.LastHeartbeatTime));
        }

        static void Main()
        {
            // 服务端
            Server server = new Server();

            // 服务端离线事件
            server.OnClientOffline += ClientOffline;

            // 服务器上线事件
            server.OnClientOnline += ClientOnline;

            // 开启服务器
            server.Start();

            // 模拟100个客户端
            Dictionary<Int32, Client> dicClient = new Dictionary<Int32, Client>();
            for (Int32 i = 0; i < 100; i++)
            {
                // 这里传入server只是为了方便而已
                Client client = new Client(i + 1, server);
                dicClient.Add(i + 1, client);

                // 开启客户端
                client.Start();
            }

            System.Threading.Thread.Sleep(1000);

            while (true)
            {
                Console.WriteLine("请输入要离线的ClientID,输入0则退出程序:");
                String clientID = Console.ReadLine();
                if (!String.IsNullOrEmpty(clientID))
                {
                    Int32 iClientID = 0;
                    Int32.TryParse(clientID, out iClientID);
                    if (iClientID > 0)
                    {
                        Client client;
                        if (dicClient.TryGetValue(iClientID, out client))
                        {
                            // 客户端离线
                            client.Offline = true;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
    }

    /// <summary>
    /// 服务端
    /// </summary>
    public class Server
    {
        public event ClientOfflineHandler OnClientOffline;
        public event ClientOnlineHandler OnClientOnline;

        private Dictionary<Int32, ClientInfo> _DicClient;

        /// <summary>
        /// 构造函数
        /// </summary>
        public Server()
        {
            _DicClient = new Dictionary<Int32, ClientInfo>(100);            
        }

        /// <summary>
        /// 开启服务端
        /// </summary>
        public void Start()
        {
            // 开启扫描离线线程
            Thread t = new Thread(new ThreadStart(ScanOffline));
            t.IsBackground = true;
            t.Start();
        }

        /// <summary>
        /// 扫描离线
        /// </summary>
        private