我需要获得异步套接字服务器的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