/// <summary> /// 给指定的客户端发送消息 /// </summary> /// <param name="ip"></param> /// <param name="msg"></param> public void PutMessage(string ip, string msg) { lock (receiveLock) { try { SocketTime socketTime = null; if (dicSocket.TryGetValue(ip, out socketTime)) { byte[] buffer = System.Text.Encoding.UTF8.GetBytes(msg); socketTime.socket.Send(buffer); } } catch (Exception ex) { log.Error(ex); } } }
/// <summary> /// 判断客户端是否超时,并将超时的客户端移除 /// </summary> public void ClientTimeOut() { while (true) { foreach (KeyValuePair <string, SocketTime> s in dicSocket) { TimeSpan ts = DateTime.Now - s.Value.dateTime; //移除超时的客户端 if (ts.TotalSeconds > clientTimeOut) { SocketTime st = null; //移除IP ComboxIp(false, s.Key); dicSocket.TryRemove(s.Key, out st); log.Info("Remove:" + s.Key); ShowMessage(s.Key + "客户端失去连接!\r\n"); } } Thread.Sleep(10); } }