server的代码 public abstract class Server { static readonly ILog logger = LogManager.GetLogger(typeof(Server)); public int Port { get; set; } public event ClientEventHandler OnClientAcceptEvent; public event ClientEventHandler OnClientOnlineEvent; public event ClientEventHandler OnClientRemoveEvent; private bool bStarted; static private int NextClientId = 0; private TcpListener _listener; protected Dictionaryid2client = new Dictionary (); private List noHeartBeatClients = new List (); public bool HasHeartBeat; private int CheckHeartBeatInterval = 30 * 1000;// ms protected System.Timers.Timer CheckHeartBeatTimer = new System.Timers.Timer(); private object id2clientCS = new object(); private object noHeartBeatClientsCS = new object(); private Server() { CheckHeartBeatTimer.Elapsed += (o1, a1) => { if(HasHeartBeat == false) return; List kickClientList = new List (); lock(id2clientCS) { try { DateTime now = DateTime.Now; foreach(KeyValuePair pair in id2client) { try { Client client = pair.Value; TimeSpan offset = now - client.LastHeartBeat; if(client != null && client.State == Client.ConnectionState.Connected && offset.TotalMilliseconds > CheckHeartBeatInterval) { kickClientList.Add(pair.Value); logger.InfoFormat("检测到心跳超时: [IP]{0}, [ID]{1}, [Time]{2}", client.ClientIpAddress, pair.Key, DateTime.Now.ToString()); } } catch { } } } catch(Exception ex) { logger.WarnFormat("心跳检测时发生异常: \n{0}", ex); } } kickClientList.ForEach(p => p.Close()); lock(noHeartBeatClientsCS) { kickClientList.ForEach(c => noHeartBeatClients.RemoveAll(p => p.Id == c.Id)); } }; } public Server(int port) : this() { this.Port = port; } public List Clients { get { List result = new List (); lock(id2clientCS) { foreach(Client each in id2client.Values) { result.Add(each); } } return result; } } public virtual void Open() { _listener = new TcpListener(Port); logger.InfoFormat("Server#Open port={0}", Port); try { _listener.Start(); if(HasHeartBeat) { CheckHeartBeatTimer.Stop(); CheckHeartBeatTimer.Interval = CheckHeartBeatInterval; CheckHeartBeatTimer.Start(); } _listener.BeginAcceptTcpClient(new AsyncCallback(OnAccept), null); bStarted = true; } catch(SocketException ex) { logger.WarnFormat("服务器监听发生异常:{0}\nSocket ErrorCode: {1}\n提示:请检查端口是否已被占用", ex.Message, ex.ErrorCode); throw ex; } catch(Exception ex) { logger.Warn(ex); throw ex; } } public virtual void Close() { try { if(HasHeartBeat) { CheckHeartBeatTimer.Stop(); } _listener.Stop(); bStarted = false; lock(id2clientCS) { foreach(Client each in id2client.Values) { try { if(each != null)each.Close(); } catch { } } id2client.Clear(); } } catch(Exception ex) { logger.Warn(ex); throw ex; } } private void OnAccept(IAsyncResult ar) { try { Client client = CreateClient(NextClientId++, _listener.EndAcceptTcpClient(ar), this); client.LastHeartBeat = DateTime.Now; client.PostOfflineEvent += (obj, args) => RemoveClient(client); lock(id2clientCS) { id2client.Add(client.Id, client); } lock(noHeartBeatClientsCS) { noHeartBeatClients.Add(client); } if(OnClientAcceptEvent != null) OnClientAcceptEvent(client); } catch(ObjectDisposedException) { } catch(Exception ex) { logger.Warn(ex); } finally { try { _listener.BeginAcceptTcpClient(new AsyncCallback(OnAccept), null); } catch(Exception) { // ignore } } } protected abstract Client CreateClient(int id, TcpClient tcpClient, Server server); public void RemoveClient(Client client) { if(bStarted == false) return; lock(id2clientCS) { id2client.Remove(client.Id); } if(OnClientRemoveEvent != null) OnClientRemoveEvent(client); } public void GetHeartBeat(Client client) { if(HasHeartBeat == false) return; client.LastHeartBeat = DateTime.Now; lock(noHeartBeatClientsCS) { int index = noHeartBeatClients.FindIndex(p => p.Id == client.Id); if(index == -1) return; try { if(OnClientOnlineEvent != null) OnClientOnlineEvent(client); } catch(Exception ex) { logger.Warn(ex); } noHeartBeatClients.RemoveAt(index); } }}
-----解决方案--------------------------------------------------------
在定时器里隔段时间就发送几个字节的数据。如果3次没有返回则断开 ------解决方案--------------------------------------------------------客户端:30秒发送一个心跳包到服务器服务器:接收到心跳包,更新LastHeartbeatTime并且有一个线程,一分钟扫描一次,如果LastHeartbeatTime超过一分钟没更新的视为下线 ------解决方案--------------------------------------------------------模拟心跳的机制使用直接调用函数模拟心跳,没有涉及到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 { ////// 客户端离线提示 /// /// private static void ClientOffline(ClientInfo clientInfo) { Console.WriteLine(String.Format("客户端{0}离线,离线时间:\t{1}", clientInfo.ClientID, clientInfo.LastHeartbeatTime)); } ////// 客户端上线提示 /// /// 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个客户端 DictionarydicClient = new Dictionary (); 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; } } } } } /// /// 服务端 /// public class Server { public event ClientOfflineHandler OnClientOffline; public event ClientOnlineHandler OnClientOnline; private Dictionary_DicClient; /// /// 构造函数 /// public Server() { _DicClient = new Dictionary(100); } /// /// 开启服务端 /// public void Start() { // 开启扫描离线线程 Thread t = new Thread(new ThreadStart(ScanOffline)); t.IsBackground = true; t.Start(); } ////// 扫描离线 /// private void ScanOffline() { while (true) { // 一秒扫描一次 System.Threading.Thread.Sleep(1000); lock (_DicClient) { foreach (Int32 clientID in _DicClient.Keys) { ClientInfo clientInfo = _DicClient[clientID]; // 如果已经离线则不用管 if (!clientInfo.State) { continue; } // 判断最后心跳时间是否大于3秒 TimeSpan sp = System.DateTime.Now - clientInfo.LastHeartbeatTime; if (sp.Seconds >= 3) { // 离线,触发离线事件 if (OnClientOffline != null) { OnClientOffline(clientInfo); } // 修改状态 clientInfo.State = false; } } } } } ////// 接收心跳包 /// /// 客户端ID public void ReceiveHeartbeat(Int32 clientID) { lock (_DicClient) { ClientInfo clientInfo; if (_DicClient.TryGetValue(clientID, out clientInfo)) { // 如果客户端已经上线,则更新最后心跳时间 clientInfo.LastHeartbeatTime = System.DateTime.Now; } else { // 客户端不存在,则认为是新上线的 clientInfo = new ClientInfo(); clientInfo.ClientID = clientID; clientInfo.LastHeartbeatTime = System.DateTime.Now; clientInfo.State = true; _DicClient.Add(clientID, clientInfo); // 触发上线事件 if (OnClientOnline != null) { OnClientOnline(clientInfo); } } } } } ////// 客户端 /// public class Client { public Server Server; public Int32 ClientID; public Boolean Offline; ////// 构造函数 /// /// /// public Client(Int32 clientID, Server server) { ClientID = clientID; Server = server; Offline = false; } ////// 开启客户端 /// public void Start() { // 开启心跳线程 Thread t = new Thread(new ThreadStart(Heartbeat)); t.IsBackground = true; t.Start(); } ////// 向服务器发送心跳包 /// private void Heartbeat() { while (!Offline) { // 向服务端发送心跳包 Server.ReceiveHeartbeat(ClientID); System.Threading.Thread.Sleep(1000); } } } ////// 客户端信息 /// public class ClientInfo { // 客户端ID public Int32 ClientID; // 最后心跳时间 public DateTime LastHeartbeatTime; // 状态 public Boolean State; }}