Connection reset when receiving a packet in a UDP server

I am working on a server application (C #, .NET 4.0) that needs to process thousands of UDP packets every second. So I decided SocketAsyncEventArg implement the server.

The problem I am facing is that my implementation receives only one packet, and then I get the "ConnectionReset" error (I never thought I could get this error because UDP is without a connection). Here is my test implementation:

 using System; using System.Net; using System.Net.Sockets; static class Program { static void Main(string[] args) { UdpEchoServer.Start(); while (true) { Console.ReadLine(); SendPacket(); } } static void SendPacket() { Console.WriteLine("SendPacket"); var c = new UdpClient(); c.Send(new byte[5], 5, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 445)); c.Close(); } } static class UdpEchoServer { static Socket mSocket; static byte[] mBuffer; static SocketAsyncEventArgs mRxArgs, mTxArgs; static IPEndPoint mAnyEndPoint, mLocalEndPoint; public static void Start() { mAnyEndPoint = new IPEndPoint(IPAddress.Any, 0); mLocalEndPoint = new IPEndPoint(IPAddress.Any, 445); mBuffer = new byte[1024]; mRxArgs = new SocketAsyncEventArgs(); mTxArgs = new SocketAsyncEventArgs(); mRxArgs.Completed += ReceiveComplete; mTxArgs.Completed += SendComplete; mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); mSocket.Bind(mLocalEndPoint); ReceiveNext(); } static void ReceiveNext() { Console.WriteLine("ReceiveNext"); mRxArgs.RemoteEndPoint = mAnyEndPoint; mRxArgs.SetBuffer(mBuffer, 0, mBuffer.Length); if (!mSocket.ReceiveFromAsync(mRxArgs)) Console.WriteLine("Error in ReceiveNext: " + mRxArgs.SocketError); } static void ReceiveComplete(object sender, SocketAsyncEventArgs e) { Console.WriteLine("Receive Complete: " + mRxArgs.SocketError); if (mRxArgs.SocketError != SocketError.Success) return; mTxArgs.SetBuffer(mBuffer, 0, mRxArgs.BytesTransferred); mTxArgs.RemoteEndPoint = mRxArgs.RemoteEndPoint; Console.WriteLine("Sending reply packet"); if (!mSocket.SendToAsync(mTxArgs)) Console.WriteLine("Error in ReceiveComplete: " + mRxArgs.SocketError); } static void SendComplete(object sender, SocketAsyncEventArgs e) { Console.WriteLine("Send Complete: " + mTxArgs.SocketError); if (mTxArgs.SocketError != SocketError.Success) return; ReceiveNext(); } } 

Sorry for the long code, but it is very simple. I wait for the packet, respond to the remote endpoint, and then wait for the next. Here is the result:

 ReceiveNext SendPacket Receive Complete: Success Sending reply packet Send Complete: Success ReceiveNext Error in ReceiveNext: ConnectionReset 

Please, can you suggest what is wrong in the code snippet above?

+6
source share
2 answers

This happens with UDP sockets. All you have to do is change the socket mode before binding it. Use this code in the Start method.

 mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); const int SIO_UDP_CONNRESET = -1744830452; byte[] inValue = new byte[] {0}; byte[] outValue = new byte[] {0}; mSocket.IOControl(SIO_UDP_CONNRESET, inValue, outValue); mSocket.Bind(mLocalEndPoint); 
+10
source

If you delete or comment out the Close call on UdpClient , then the program works as expected. Why this is happening, I'm not sure, but it could be due to the Windows network loop.

+1
source

Source: https://habr.com/ru/post/914217/


All Articles