C#基于UDP进行异步通信的方法
程序员文章站
2022-06-03 18:46:09
本文实例讲述了c#基于udp进行异步通信的方法。分享给大家供大家参考。具体如下:
服务器端:
using system;
using system.colle...
本文实例讲述了c#基于udp进行异步通信的方法。分享给大家供大家参考。具体如下:
服务器端:
using system; using system.collections.generic; using system.text; using system.net; using system.net.sockets; using system.threading; namespace asyncserver { public class udpstate { public udpclient udpclient; public ipendpoint ipendpoint; public const int buffersize = 1024; public byte[] buffer = new byte[buffersize]; public int counter = 0; } public class asyncudpsever { private ipendpoint ipendpoint = null; private ipendpoint remoteep = null; private udpclient udpreceive = null; private udpclient udpsend = null; private const int listenport = 1100; private const int remoteport = 1101; udpstate udpreceivestate = null; udpstate udpsendstate = null; private manualresetevent senddone = new manualresetevent(false); private manualresetevent receivedone = new manualresetevent(false); public asyncudpsever() { ipendpoint = new ipendpoint(ipaddress.any, listenport); remoteep = new ipendpoint(dns.gethostaddresses(dns.gethostname())[0], remoteport); udpreceive = new udpclient(ipendpoint); udpsend = new udpclient(); udpreceivestate = new udpstate(); udpreceivestate.udpclient = udpreceive; udpreceivestate.ipendpoint = ipendpoint; udpsendstate = new udpstate(); udpsendstate.udpclient = udpsend; udpsendstate.ipendpoint = remoteep; } public void receivemsg() { console.writeline("listening for messages"); while(true) { lock (this) { iasyncresult iar = udpreceive.beginreceive(new asynccallback(receivecallback), udpreceivestate); receivedone.waitone(); thread.sleep(100); } } } private void receivecallback(iasyncresult iar) { udpstate udpreceivestate = iar.asyncstate as udpstate; if (iar.iscompleted) { byte[] receivebytes = udpreceivestate.udpclient.endreceive(iar, ref udpreceivestate.ipendpoint); string receivestring = encoding.ascii.getstring(receivebytes); console.writeline("received: {0}", receivestring); //thread.sleep(100); receivedone.set(); sendmsg(); } } private void sendmsg() { udpsend.connect(udpsendstate.ipendpoint); udpsendstate.udpclient = udpsend; udpsendstate.counter ++; string message = string.format("第{0}个udp请求处理完成!",udpsendstate.counter); byte[] sendbytes = encoding.unicode.getbytes(message); udpsend.beginsend(sendbytes, sendbytes.length, new asynccallback(sendcallback), udpsendstate); senddone.waitone(); } private void sendcallback(iasyncresult iar) { udpstate udpstate = iar.asyncstate as udpstate; console.writeline("第{0}个请求处理完毕!", udpstate.counter); console.writeline("number of bytes sent: {0}", udpstate.udpclient.endsend(iar)); senddone.set(); } public static void main() { asyncudpsever aus = new asyncudpsever(); thread t = new thread(new threadstart(aus.receivemsg)); t.start(); console.read(); } } }
客户端:
using system; using system.collections.generic; using system.text; using system.net; using system.net.sockets; using system.threading; namespace asyncclient { public class udpstate { public udpclient udpclient = null; public ipendpoint ipendpoint = null; public const int buffersize = 1024; public byte[] buffer = new byte[buffersize]; public int counter = 0; } public class asyncudpclient { public static bool messagesent = false; // receive a message and write it to the console. private const int listenport = 1101; private const int remoteport = 1100; private ipendpoint localep = null; private ipendpoint remoteep = null; private udpclient udpreceive = null; private udpclient udpsend = null; private udpstate udpsendstate = null; private udpstate udpreceivestate = null; private int counter = 0; private manualresetevent senddone = new manualresetevent(false); private manualresetevent receivedone = new manualresetevent(false); private socket receivesocket; private socket sendsocket; public asyncudpclient() { localep = new ipendpoint(ipaddress.any, listenport); remoteep = new ipendpoint(dns.gethostaddresses(dns.gethostname())[0],remoteport); udpreceive = new udpclient(localep); udpsend = new udpclient(); udpsendstate = new udpstate(); udpsendstate.ipendpoint = remoteep; udpsendstate.udpclient = udpsend; udpreceivestate = new udpstate(); udpreceivestate.ipendpoint = remoteep; udpreceivestate.udpclient = udpreceive; receivesocket = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp); receivesocket.bind(localep); sendsocket = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp); sendsocket.bind(remoteep); } public void sendmsg() { udpsend.connect(remoteep); //thread t = new thread(new threadstart(receivemessages)); //t.start(); byte[] sendbytes; string message; while (true) { message = "client" + (counter++).tostring(); lock (this) { sendbytes = encoding.ascii.getbytes(message); udpsendstate.counter = counter; udpsend.beginsend(sendbytes, sendbytes.length, new asynccallback(sendcallback), udpsendstate); senddone.waitone(); thread.sleep(200); receivemessages(); } } } public void sendcallback(iasyncresult iar) { udpstate udpstate = iar.asyncstate as udpstate; if (iar.iscompleted) { console.writeline("第{0}个发送完毕!", udpstate.counter); console.writeline("number of bytes sent: {0}", udpstate.udpclient.endsend(iar)); //if (udpstate.counter == 10) //{ // udpstate.udpclient.close(); //} senddone.set(); } } public void receivemessages() { lock (this) { udpreceive.beginreceive(new asynccallback(receivecallback), udpreceivestate); receivedone.waitone(); thread.sleep(100); } } public void receivecallback(iasyncresult iar) { udpstate udpstate = iar.asyncstate as udpstate; if (iar.iscompleted) { byte[] receivebytes = udpstate.udpclient.endreceive(iar, ref udpreceivestate.ipendpoint); string receivestring = encoding.unicode.getstring(receivebytes); console.writeline("received: {0}", receivestring); receivedone.set(); } } public static void main() { asyncudpclient auc = new asyncudpclient(); auc.sendmsg(); console.read(); } } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: jquery select选中的一个小问题_jquery
下一篇: C#线程池操作方法