C#使用Socket实现心跳的方法示例-创新互联
Server端代码:
黄石港网站建设公司创新互联,黄石港网站设计制作,有大型网站制作公司丰富经验。已为黄石港上1000家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的黄石港做网站的公司定做!class Program { static SocketListener listener; public static void Main(string[] args) { //实例化Timer类,设置间隔时间为5000毫秒; System.Timers.Timer t = new System.Timers.Timer(5000); t.Elapsed += new System.Timers.ElapsedEventHandler(CheckListen); //到达时间的时候执行事件; t.AutoReset = true; t.Start(); listener = new SocketListener(); listener.ReceiveTextEvent += new SocketListener.ReceiveTextHandler(ShowText); listener.StartListen(); Console.ReadKey(); } private static void ShowText(string text) { Console.WriteLine(text); } private static void CheckListen(object sender, System.Timers.ElapsedEventArgs e) { if (listener != null && listener.Connection != null) { Console.WriteLine("连接数:" + listener.Connection.Count.ToString()); } } } public class Connection { Socket _connection; public Connection(Socket socket) { _connection = socket; } public void WaitForSendData(object connection) { try { while (true) { byte[] bytes = new byte[1024]; string data = ""; //等待接收消息 int bytesRec = this._connection.Receive(bytes); if (bytesRec == 0) { // ReceiveText("客户端[" + _connection.RemoteEndPoint.ToString() + "]连接关闭..."); break; } data += Encoding.UTF8.GetString(bytes, 0, bytesRec); ReceiveText("收到消息:" + data); string sendStr = "服务端已经收到信息!"; byte[] bs = Encoding.UTF8.GetBytes(sendStr); _connection.Send(bs, bs.Length, 0); } } catch (Exception) { ReceiveText("客户端[" + _connection.RemoteEndPoint.ToString() + "]连接已断开..."); Hashtable hConnection = connection as Hashtable; if (hConnection.Contains(_connection.RemoteEndPoint.ToString())) { hConnection.Remove(_connection.RemoteEndPoint.ToString()); } } } public delegate void ReceiveTextHandler(string text); public event ReceiveTextHandler ReceiveTextEvent; private void ReceiveText(string text) { if (ReceiveTextEvent != null) { ReceiveTextEvent(text); } } } public class SocketListener { public Hashtable Connection = new Hashtable(); public void StartListen() { Agine: try { //端口号、IP地址 //int port = 8889; //string host = "127.0.0.1"; //IPAddress ip = IPAddress.Parse(host); //IPEndPoint ipe = new IPEndPoint(ip, port); string ip = string.Empty; System.Net.IPHostEntry IpEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()); for (int i = 0; i != IpEntry.AddressList.Length; i++) { if (!IpEntry.AddressList[i].IsIPv6LinkLocal) { ip = IpEntry.AddressList[i].ToString(); } } IPEndPoint ipend = new IPEndPoint(IPAddress.Parse(ip), 6000); //创建一个Socket类 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Bind(ipend);//绑定2000端口 s.Listen(0);//开始监听 ReceiveText("启动Socket监听..."); while (true) { Socket connectionSocket = s.Accept();//为新建连接创建新的Socket ReceiveText("客户端[" + connectionSocket.RemoteEndPoint.ToString() + "]连接已建立..."); Connection gpsCn = new Connection(connectionSocket); gpsCn.ReceiveTextEvent += new Connection.ReceiveTextHandler(ReceiveText); Connection.Add(connectionSocket.RemoteEndPoint.ToString(), gpsCn); //在新线程中启动新的socket连接,每个socket等待,并保持连接 Thread thread = new Thread(gpsCn.WaitForSendData); thread.Name = connectionSocket.RemoteEndPoint.ToString(); thread.Start(Connection); } } catch (ArgumentNullException ex1) { ReceiveText("ArgumentNullException:" + ex1); } catch (SocketException ex2) { ReceiveText("SocketException:" + ex2); } goto Agine; } public delegate void ReceiveTextHandler(string text); public event ReceiveTextHandler ReceiveTextEvent; private void ReceiveText(string text) { if (ReceiveTextEvent != null) { ReceiveTextEvent(text); } } }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站栏目:C#使用Socket实现心跳的方法示例-创新互联
网站链接:http://hbruida.cn/article/dssgsh.html