C#基于UDP进行异步通信的方法
本文实例讲述了C#基于UDP进行异步通信的方法。分享给大家供大家参考。具体如下:
服务器端:
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Net; usingSystem.Net.Sockets; usingSystem.Threading; namespaceAsyncServer { publicclassUdpState { publicUdpClientudpClient; publicIPEndPointipEndPoint; publicconstintBufferSize=1024; publicbyte[]buffer=newbyte[BufferSize]; publicintcounter=0; } publicclassAsyncUdpSever { privateIPEndPointipEndPoint=null; privateIPEndPointremoteEP=null; privateUdpClientudpReceive=null; privateUdpClientudpSend=null; privateconstintlistenPort=1100; privateconstintremotePort=1101; UdpStateudpReceiveState=null; UdpStateudpSendState=null; privateManualResetEventsendDone=newManualResetEvent(false); privateManualResetEventreceiveDone=newManualResetEvent(false); publicAsyncUdpSever() { ipEndPoint=newIPEndPoint(IPAddress.Any,listenPort); remoteEP=newIPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0],remotePort); udpReceive=newUdpClient(ipEndPoint); udpSend=newUdpClient(); udpReceiveState=newUdpState(); udpReceiveState.udpClient=udpReceive; udpReceiveState.ipEndPoint=ipEndPoint; udpSendState=newUdpState(); udpSendState.udpClient=udpSend; udpSendState.ipEndPoint=remoteEP; } publicvoidReceiveMsg() { Console.WriteLine("listeningformessages"); while(true) { lock(this) { IAsyncResultiar=udpReceive.BeginReceive(newAsyncCallback(ReceiveCallback),udpReceiveState); receiveDone.WaitOne(); Thread.Sleep(100); } } } privatevoidReceiveCallback(IAsyncResultiar) { UdpStateudpReceiveState=iar.AsyncStateasUdpState; if(iar.IsCompleted) { Byte[]receiveBytes=udpReceiveState.udpClient.EndReceive(iar,refudpReceiveState.ipEndPoint); stringreceiveString=Encoding.ASCII.GetString(receiveBytes); Console.WriteLine("Received:{0}",receiveString); //Thread.Sleep(100); receiveDone.Set(); SendMsg(); } } privatevoidSendMsg() { udpSend.Connect(udpSendState.ipEndPoint); udpSendState.udpClient=udpSend; udpSendState.counter++; stringmessage=string.Format("第{0}个UDP请求处理完成!",udpSendState.counter); Byte[]sendBytes=Encoding.Unicode.GetBytes(message); udpSend.BeginSend(sendBytes,sendBytes.Length,newAsyncCallback(SendCallback),udpSendState); sendDone.WaitOne(); } privatevoidSendCallback(IAsyncResultiar) { UdpStateudpState=iar.AsyncStateasUdpState; Console.WriteLine("第{0}个请求处理完毕!",udpState.counter); Console.WriteLine("numberofbytessent:{0}",udpState.udpClient.EndSend(iar)); sendDone.Set(); } publicstaticvoidMain() { AsyncUdpSeveraus=newAsyncUdpSever(); Threadt=newThread(newThreadStart(aus.ReceiveMsg)); t.Start(); Console.Read(); } } }
客户端:
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Net; usingSystem.Net.Sockets; usingSystem.Threading; namespaceAsyncClient { publicclassUdpState { publicUdpClientudpClient=null; publicIPEndPointipEndPoint=null; publicconstintBufferSize=1024; publicbyte[]buffer=newbyte[BufferSize]; publicintcounter=0; } publicclassAsyncUdpClient { publicstaticboolmessageSent=false; //Receiveamessageandwriteittotheconsole. privateconstintlistenPort=1101; privateconstintremotePort=1100; privateIPEndPointlocalEP=null; privateIPEndPointremoteEP=null; privateUdpClientudpReceive=null; privateUdpClientudpSend=null; privateUdpStateudpSendState=null; privateUdpStateudpReceiveState=null; privateintcounter=0; privateManualResetEventsendDone=newManualResetEvent(false); privateManualResetEventreceiveDone=newManualResetEvent(false); privateSocketreceiveSocket; privateSocketsendSocket; publicAsyncUdpClient() { localEP=newIPEndPoint(IPAddress.Any,listenPort); remoteEP=newIPEndPoint(Dns.GetHostAddresses(Dns.GetHostName())[0],remotePort); udpReceive=newUdpClient(localEP); udpSend=newUdpClient(); udpSendState=newUdpState(); udpSendState.ipEndPoint=remoteEP; udpSendState.udpClient=udpSend; udpReceiveState=newUdpState(); udpReceiveState.ipEndPoint=remoteEP; udpReceiveState.udpClient=udpReceive; receiveSocket=newSocket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); receiveSocket.Bind(localEP); sendSocket=newSocket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); sendSocket.Bind(remoteEP); } publicvoidSendMsg() { udpSend.Connect(remoteEP); //Threadt=newThread(newThreadStart(ReceiveMessages)); //t.Start(); Byte[]sendBytes; stringmessage; while(true) { message="Client"+(counter++).ToString(); lock(this) { sendBytes=Encoding.ASCII.GetBytes(message); udpSendState.counter=counter; udpSend.BeginSend(sendBytes,sendBytes.Length,newAsyncCallback(SendCallback),udpSendState); sendDone.WaitOne(); Thread.Sleep(200); ReceiveMessages(); } } } publicvoidSendCallback(IAsyncResultiar) { UdpStateudpState=iar.AsyncStateasUdpState; if(iar.IsCompleted) { Console.WriteLine("第{0}个发送完毕!",udpState.counter); Console.WriteLine("numberofbytessent:{0}",udpState.udpClient.EndSend(iar)); //if(udpState.counter==10) //{ //udpState.udpClient.Close(); //} sendDone.Set(); } } publicvoidReceiveMessages() { lock(this) { udpReceive.BeginReceive(newAsyncCallback(ReceiveCallback),udpReceiveState); receiveDone.WaitOne(); Thread.Sleep(100); } } publicvoidReceiveCallback(IAsyncResultiar) { UdpStateudpState=iar.AsyncStateasUdpState; if(iar.IsCompleted) { Byte[]receiveBytes=udpState.udpClient.EndReceive(iar,refudpReceiveState.ipEndPoint); stringreceiveString=Encoding.Unicode.GetString(receiveBytes); Console.WriteLine("Received:{0}",receiveString); receiveDone.Set(); } } publicstaticvoidMain() { AsyncUdpClientauc=newAsyncUdpClient(); auc.SendMsg(); Console.Read(); } } }
希望本文所述对大家的C#程序设计有所帮助。