远程主机强制关闭现有连接

时间:2011-08-26 08:20:18

标签: c# sockets asynchronous udp

我需要从异步套接字服务器获取UDP数据报,但我的应用程序中发生异常:

出现问题:

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

完整的源代码:

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());

            }
        }
    }

例外:

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

可能是什么问题?


要提供更多信息,我在此页面上购买了私人网袜连接:http://rapidsocks.com/ 这个服务给我一个IP和端口列表谁真的不是代理..只是一个连接,给我一个代理服务器:来自服务器池上的proxyPort响应...

如何从服务器上获取proxyIP:proxyPort的答案?

2 个答案:

答案 0 :(得分:44)

在UDP域中,可能出现的一种方法是将UDP数据包发送到主机,并且远程主机在该端口上没有侦听器,并在响应时退回ICMP主机不可达消息。

用简单的英语,这个异常告诉你没有进程正在该端口上的远端侦听。


更新:您应该能够使用以下代码避免该行为:

  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);

微软第263823条就此主题发表了这样的话:[截至2019年很难找到]

  

症状   在Windows 2000中,用户数据报协议(UDP)程序可以   不起作用,可能会产生WSAECONNRESET响应。

     

原因   如果使用sendto函数发送数据报导致   “ICMP端口不可达”响应和选择功能设置为   readfds,程序返回1并随后调用recvfrom   函数不适用于WSAECONNRESET(10054)错误响应。在   Microsoft Windows NT 4.0,这种情况导致select函数   阻止或超时。

     

解决方案   新的套接字IOCTL称为“SIO_UDP_CONNRESET”   在Windows 2000中引入。当使用此IOCTL时,程序必须   专门为Windows 2000重写以获取原始版本   Windows NT 4.0行为。 Windows NT 4.0,Microsoft Windows 95和   Microsoft Windows 98不支持这个新的IOCTL。此外   要重写您的应用程序,您将需要引用的修补程序   在本文中进一步说明。

答案 1 :(得分:3)

这确实是一个通用的错误消息,可能意味着什么。是时候让低级网络流量嗅探器过滤实际出错的地方了。添加额外的错误处理尝试使用具有良好日志记录的服务器上的catch块始终是一个很好的起点。