An existing connection was forcibly closed by the

2019-02-08 13:02发布

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?

2条回答
SAY GOODBYE
2楼-- · 2019-02-08 13:20

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.

查看更多
在下西门庆
3楼-- · 2019-02-08 13:31

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.

http://support.microsoft.com/kb/263823/en-us

This exception tells you that no process is listening on that port.


Update: You should be able to avoid that behaviour 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);
查看更多
登录 后发表回答