2011-08-26 104 views
14

我需要获得异步套接字服务器的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列表和端口谁在真不是代理..只是一个方面,给我一个proxyIP:从服务器池中的proxyPort作为响应...

如何使用proxyIP:proxyPort从服务器获取该答案?

+0

很好的问题 - 也许,如果你告诉我们多一点 - 在被抛出的异常?你在控制台上看到任何“调试消息”吗?你能告诉我们一个测试运行吗? – Carsten

+0

请在catch块中做一个stackTrace打印,看看异常抛出的是哪一行。 – Zenwalker

+0

*对方*正常工作 - 是吗?你能检查一下吗? – Carsten

回答

3

这确实是一个通用的错误消息,可能意味着什么。有时间让低层网络流量监听器过滤出实际出错的地方。添加额外的错误处理在体面的日志记录服务器上尝试catch块总是一个很好的开始。

+0

我不是一个代理服务器的服务器,它是私人的,任何信息如何获得IP:内部端口写入rfc1928 – Johnny

+0

CONNECT 在对CONNECT的回复中,BND.PORT包含 服务器分配用于连接到目标主机的端口号,而BND.ADDR 包含关联的IP地址。提供的BND.ADDR通常是 ,与客户端用于访问SOCKS 服务器的IP地址不同,因为这些服务器通常是多宿主服务器。预计 表示SOCKS服务器将使用DST.ADDR和DST.PORT,并在评估CONNECT 请求时使用客户端源地址和端口。 – Johnny

+0

服务器或代理是否有任何异常? – CodingBarfield

41

在UDP域中,出现这种情况的一种方法是当您向某个主机发送一个UDP数据包,并且远程主机在该端口上没有侦听器,并且弹出ICMP主机不可达消息作为响应。

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

此异常告诉你,没有进程正在该端口上侦听。


更新:你应该能够避免与下面的代码的行为:

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

对我来说,这解决了Send()在没有侦听器的情况下将数据报发送到套接字后发生Receive()异常的问题。我可以发送()许多数据报没有监听器没有错误,但Receive()将不再工作。非常模糊的错误行为。这个解决方案同样含糊不清,但它非常棒! +1 – Roland

+0

发生此错误时,该字段中的设备突然更改IP地址,而UDP数据包未完成。这固定了它。 – SteveGSD

相关问题