C#使用Socket实现心跳的方法示例
程序员文章站
2023-11-19 15:27:16
server端代码:class program{ static socketlistener listener; public static void main(string[] args)...
server端代码:
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); } } }
client端代码:
class program { static void main(string[] args) { socket c; //int port = 4029; // 避免使用127.0.0.1,我在本机测试是不能运行的 //string host = "127.0.0.1"; //ipaddress ip = ipaddress.parse(host); //ipendpoint ipe = new ipendpoint(ip, port);//把ip和端口转化为ipendpoint实例 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); c = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);//创建一个socket try { c.connect(ipend);//连接到服务器 console.writeline("连接到socket服务端..."); console.writeline("发送消息到服务端..."); string sendstr = "m s g"; byte[] bs = encoding.utf8.getbytes(sendstr); c.send(bs, bs.length, 0); string recvstr = ""; byte[] recvbytes = new byte[1024]; int bytes; bytes = c.receive(recvbytes, recvbytes.length, 0);//从服务器端接受返回信息 recvstr += encoding.utf8.getstring(recvbytes, 0, bytes); console.writeline("服务器返回信息:" + recvstr); } catch (argumentnullexception ex1) { console.writeline("argumentnullexception:{0}", ex1); } catch (socketexception ex2) { console.writeline("socketexception:{0}", ex2); } console.readkey(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。