我需要獲得異步套接字服務器的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從服務器獲取該答案?
很好的問題 - 也許,如果你告訴我們多一點 - 在被拋出的異常?你在控制檯上看到任何「調試消息」嗎?你能告訴我們一個測試運行嗎? – Carsten
請在catch塊中做一個stackTrace打印,看看異常拋出的是哪一行。 – Zenwalker
*對方*正常工作 - 是嗎?你能檢查一下嗎? – Carsten