How to write a Async UDP socket code?
-
I want to create a Listening UDP socket in Async Mode. here is my code: private void button7_Click(object sender, System.EventArgs e) { sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); iep = new IPEndPoint(IPAddress.Any,20100); sock.Bind(iep); sock.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock); } private void ReceiveData(IAsyncResult iar) { Socket remote = (Socket)iar.AsyncState; int recv = remote.EndReceive(iar); Console.WriteLine("Recv {0}:",recv); remote.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock); } ////////////////////////////// i want the sock to keep listening. However, once i send a udp message with UDPClient from another pc, the ReceiveData(IAsyncResult iar) method turns into a loop keeping writeline. what is wrong ?
-
I want to create a Listening UDP socket in Async Mode. here is my code: private void button7_Click(object sender, System.EventArgs e) { sock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); iep = new IPEndPoint(IPAddress.Any,20100); sock.Bind(iep); sock.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock); } private void ReceiveData(IAsyncResult iar) { Socket remote = (Socket)iar.AsyncState; int recv = remote.EndReceive(iar); Console.WriteLine("Recv {0}:",recv); remote.BeginReceive(data,0,1024,SocketFlags.None,new AsyncCallback(ReceiveData),sock); } ////////////////////////////// i want the sock to keep listening. However, once i send a udp message with UDPClient from another pc, the ReceiveData(IAsyncResult iar) method turns into a loop keeping writeline. what is wrong ?
Is it the same data (assuming that you're sending unique data in the UDP datagrams)?
Microsoft MVP, Visual C# My Articles
-
Is it the same data (assuming that you're sending unique data in the UDP datagrams)?
Microsoft MVP, Visual C# My Articles
i send the udp datagrams only once using this code: UdpClient udp = new UdpClient(); byte[] data = new byte[]{0x11,0x22,0x33}; udp.Send(data,3,ip,port); at the server, Console keep showing this: recv :16 recv :18 recv :16 recv :16 ... it seems there ocurrs a loop in the ReceiveData(Async iar) method. i dont know what happened.
-
i send the udp datagrams only once using this code: UdpClient udp = new UdpClient(); byte[] data = new byte[]{0x11,0x22,0x33}; udp.Send(data,3,ip,port); at the server, Console keep showing this: recv :16 recv :18 recv :16 recv :16 ... it seems there ocurrs a loop in the ReceiveData(Async iar) method. i dont know what happened.
Assuming that the code you posted is the exact same code (people sometimes post sample code that is nothing like their actual code and doesn't contain the problem, so I just have to say it), I see no loop. What I would check out is the other machine's code. Perhaps it's looping infinitely and your client code is working correctly (seeing each UDP packet as it arrives).
Microsoft MVP, Visual C# My Articles
-
Assuming that the code you posted is the exact same code (people sometimes post sample code that is nothing like their actual code and doesn't contain the problem, so I just have to say it), I see no loop. What I would check out is the other machine's code. Perhaps it's looping infinitely and your client code is working correctly (seeing each UDP packet as it arrives).
Microsoft MVP, Visual C# My Articles