20

I need to obtain UDP datagram from Asynchronous Socket Server but an exception occurred in my application :

Problem appear there :

Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);

The full source code:

class Program
    {
        static void Main(string[] args)
        {
            const int PORT = 30485;
            IPAddress IP;
            IPAddress.TryParse("92.56.23.87", out IP);
            // This constructor arbitrarily assigns the local port number.
            UdpClient udpClient = new UdpClient(PORT);
            Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            try
            {
                udpClient.Connect("92.56.23.87", PORT);

                if (udpClient.Client.Connected)
                    Console.WriteLine("Connected.");

                // Sends a message to the host to which you have connected.
                Byte[] sendBytes = Encoding.ASCII.GetBytes("CONNECT");

                udpClient.Send(sendBytes, sendBytes.Length);

                //IPEndPoint object will allow us to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IP, PORT);

                // Blocks until a message returns on this socket from a remote host.
                Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
                string returnData = Encoding.ASCII.GetString(receiveBytes);
                // Uses the IPEndPoint object to determine which of these two hosts responded.
                Console.WriteLine("This is the message you received " + returnData.ToString());
                Console.WriteLine("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString());

                udpClient.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
        }
    }

Exception:

Connected.
System.Net.Sockets.SocketException (0x80004005): An existing connection
was forcibly closed by the remote host at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP) at ystem.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP) at ConsoleApplication7.Program.Main(String[] args) in c:\users\user\documents\visual studio 2010\Projects\ConsoleApplication7\ConsoleApplication7\Program.cs

What can be the problem?


To provide more information, i bought the private socks connection on this page: http://rapidsocks.com/ this services give me a list of IP and port who in really is not a proxy .. just a connection that give me a proxyIP:proxyPort from a pool on server in response...

How to get that answer with proxyIP:proxyPort from the server?

Johnny
  • 555
  • 1
  • 5
  • 22
  • good question - maybe if you tell us a bit more - where is the exception thrown? Do you see any of your "debug messages" on the console? Can you show us a test run? – Random Dev Aug 26 '11 at 08:22
  • Please do a stackTrace print in the catch block and see which line the exception is throwing. – Zenwalker Aug 26 '11 at 08:23
  • the *other side* is working correctly - does it? Can you check this? – Random Dev Aug 26 '11 at 08:27
  • exeption thrown here : Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); this is the exeption: An existing connection was forcibly closed by the remote host – Johnny Aug 26 '11 at 08:30
  • ckoenig - do you mean on the server side? – Johnny Aug 26 '11 at 08:32
  • if yes , I think it work correctly , I bought an socks and I try to connect to the server for obtain UDP datagram (I need IP and PORT) Logically i need some form: first connection 92.56.23.87:30485 > 76.43.126.6:1080 second connection 92.56.23.87:30485 > 78.67.54.210:5847 – Johnny Aug 26 '11 at 08:39

2 Answers2

54

In UDP land, one way this can occur is when you send a UDP packet to a host, and the remote host doesn't have a listener on that port, and bounces an ICMP host unreachable message in response.

In plain English, what this exception tells you that no process is listening on the far-end on that port.


Update: You should be able to avoid that behavior with the following code:

  var udpClient = new UdpClient();
  uint IOC_IN = 0x80000000;
  uint IOC_VENDOR = 0x18000000;
  uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
  udpClient.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);

Microsoft Article 263823 said this on the subject: [hard to find as of 2019]

SYMPTOMS In Windows 2000, a User Datagram Protocol (UDP) program may not work and may generate a WSAECONNRESET response.

CAUSE If sending a datagram using the sendto function results in an "ICMP port unreachable" response and the select function is set for readfds, the program returns 1 and the subsequent call to the recvfrom function does not work with a WSAECONNRESET (10054) error response. In Microsoft Windows NT 4.0, this situation causes the select function to block or time out.

RESOLUTION A new sockets IOCTL called "SIO_UDP_CONNRESET" has been introduced in Windows 2000. When this IOCTL is used, the program must be rewritten specifically for Windows 2000 to obtain the original Windows NT 4.0 behavior. Windows NT 4.0, Microsoft Windows 95, and Microsoft Windows 98 have no support for this new IOCTL. In addition to rewriting your application, you will need the hotfix referenced further down in this article.

Cameron
  • 2,903
  • 1
  • 30
  • 31
  • For me this solved the problem that Receive() caused exception after Send() sent a datagram to a socket without listener. I could Send() many datagrams without listener without errors, but Receive() would then not work anymore. Very vague error behaviour. This solution is equally vague, but it helped great! +1 – Roland Mar 19 '15 at 17:49
  • Had this error when devices in the field would suddenly change IP addresses while UDP packet(s) were outstanding. This fixed it. – SteveGSD Mar 20 '15 at 21:07
  • @uingtea The IOCTL shown should not cause high CPU directly, it should get called and return immediately. You may want to check your code for high frequency sending loops if you are seeing CPU issues. – Cameron Jan 10 '21 at 23:16
  • nevermind, it was another error in my code. – uingtea Jan 10 '21 at 23:59
4

This really is a generic error message that could mean anything. Time to get the low level network traffic sniffers to filter what is actually going wrong. Adding extra error handling try catch blocks on the server with decent logging is always a great place to start.

CodingBarfield
  • 3,392
  • 2
  • 27
  • 54
  • i'ts a server with proxy services, it's private and any information how to get the IP:Port on internal is write in rfc1928 – Johnny Aug 26 '11 at 08:43
  • CONNECT In the reply to a CONNECT, BND.PORT contains the port number that the server assigned to connect to the target host, while BND.ADDR contains the associated IP address. The supplied BND.ADDR is often different from the IP address that the client uses to reach the SOCKS server, since such servers are often multi-homed. It is expected that the SOCKS server will use DST.ADDR and DST.PORT, and the client-side source address and port in evaluating the CONNECT request. – Johnny Aug 26 '11 at 08:43
  • Are the server or the proxy having any exceptions? – CodingBarfield Aug 26 '11 at 08:49
  • I can not know that, for me is black box server.. the support of server give me a PHP code example : – Johnny Aug 26 '11 at 08:55
  • 1
    Why is this marked as the answer? There is another answer below that goes into much better detail about this specific question. – leviathanbadger Nov 11 '15 at 05:17
  • If in .Net the server side has an unhandled exception but is not setup to send the exception back to the client this is the expected behaviour. However nice it is to jump into the way UDP works it could be unhelpfull. – CodingBarfield Jan 19 '18 at 09:44