This article describes the method of C# performing asynchronous communication based on UDP. Share it for your reference. The details are as follows:
Server side:
using System; using ; using ; using ; using ; using ; 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(, listenPort); remoteEP = new IPEndPoint((())[0], remotePort); udpReceive = new UdpClient(ipEndPoint); udpSend = new UdpClient(); udpReceiveState = new UdpState(); = udpReceive; = ipEndPoint; udpSendState = new UdpState(); = udpSend; = remoteEP; } public void ReceiveMsg() { ("listening for messages"); while(true) { lock (this) { IAsyncResult iar = (new AsyncCallback(ReceiveCallback), udpReceiveState); (); (100); } } } private void ReceiveCallback(IAsyncResult iar) { UdpState udpReceiveState = as UdpState; if () { Byte[] receiveBytes = (iar, ref ); string receiveString = (receiveBytes); ("Received: {0}", receiveString); //(100); (); SendMsg(); } } private void SendMsg() { (); = udpSend; ++; string message = ("The{0}indivualUDPRequest processing is completed!",); Byte[] sendBytes = (message); (sendBytes, , new AsyncCallback(SendCallback), udpSendState); (); } private void SendCallback(IAsyncResult iar) { UdpState udpState = as UdpState; ("The{0}indivual请求处理完毕!", ); ("number of bytes sent: {0}", (iar)); (); } public static void Main() { AsyncUdpSever aus = new AsyncUdpSever(); Thread t = new Thread(new ThreadStart()); (); (); } } }
Client:
using System; using ; using ; using ; using ; using ; 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(, listenPort); remoteEP = new IPEndPoint((())[0],remotePort); udpReceive = new UdpClient(localEP); udpSend = new UdpClient(); udpSendState = new UdpState(); = remoteEP; = udpSend; udpReceiveState = new UdpState(); = remoteEP; = udpReceive; receiveSocket = new Socket(, , ); (localEP); sendSocket = new Socket(, , ); (remoteEP); } public void SendMsg() { (remoteEP); //Thread t = new Thread(new ThreadStart(ReceiveMessages)); //(); Byte[] sendBytes; string message; while (true) { message = "Client" + (counter++).ToString(); lock (this) { sendBytes = (message); = counter; (sendBytes, , new AsyncCallback(SendCallback), udpSendState); (); (200); ReceiveMessages(); } } } public void SendCallback(IAsyncResult iar) { UdpState udpState = as UdpState; if () { ("The{0}All sent!", ); ("number of bytes sent: {0}", (iar)); //if ( == 10) //{ // (); //} (); } } public void ReceiveMessages() { lock (this) { (new AsyncCallback(ReceiveCallback), udpReceiveState); (); (100); } } public void ReceiveCallback(IAsyncResult iar) { UdpState udpState = as UdpState; if () { Byte[] receiveBytes = (iar, ref ); string receiveString = (receiveBytes); ("Received: {0}", receiveString); (); } } public static void Main() { AsyncUdpClient auc = new AsyncUdpClient(); (); (); } } }
I hope this article will be helpful to everyone's C# programming.