2011-08-26 98 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

相關問題