首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >现有连接被远程主机强制关闭。

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

Stack Overflow用户
提问于 2011-08-26 08:20:18
回答 2查看 40.5K关注 0票数 20

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

问题出现在那里:

代码语言:javascript
运行
复制
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);

完整的源代码:

代码语言:javascript
运行
复制
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());

            }
        }
    }

例外:

代码语言:javascript
运行
复制
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

有什么问题吗?

为了提供更多的信息,我在这个页面上购买了私人socks连接:http://rapidsocks.com/这个服务给我一个IP和端口列表,他们实际上不是代理。只是一个连接给我一个proxyIP:proxyPort从服务器上的一个池响应.

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-26 08:36:10

这确实是一条通用错误消息,可能意味着任何事情。是时候让低级别的网络流量嗅探器来过滤到底出了什么问题。在服务器上添加额外的错误处理、尝试捕获块和良好的日志记录始终是一个很好的起点。

票数 4
EN

Stack Overflow用户

发布于 2011-09-19 23:32:05

在UDP中,出现这种情况的一种方式是向主机发送UDP数据包,而远程主机在该端口上没有侦听器,并在响应中弹出ICMP主机不可访问的消息。

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

Update:您应该能够使用以下代码避免这种行为:

代码语言:javascript
运行
复制
  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年很难找到。

症状在Windows2000中,用户数据报协议(UDP)程序可能无法工作,并可能生成WSAECONNRESET响应。

因为如果使用sendto函数发送数据报会导致"ICMP端口不可访问“响应,并且select函数被设置为readfds,则程序返回1,随后对recvfrom函数的调用不适用于WSAECONNRESET (10054)错误响应。在MicrosoftWindowsNT4.0中,这种情况会导致select函数阻塞或超时。

解决方案一种名为"SIO_UDP_CONNRESET“的新套接字已在Windows 2000中引入。当使用此IOCTL时,必须为Windows 2000专门重写程序,以获得原始的WindowsNT4.0行为。Windows NT 4.0、Microsoft Windows 95和Microsoft Windows 98不支持此新的IOCTL。除了重写应用程序之外,您还需要本文中进一步引用的修补程序。

票数 54
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7201862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档