【发布时间】:2021-05-12 18:10:03
【问题描述】:
我需要从异步套接字服务器获取 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 从服务器获得答案?
【问题讨论】:
-
好问题 - 如果你能告诉我们更多 - 哪里抛出异常?您在控制台上看到任何“调试消息”吗?你能告诉我们一个测试运行吗?
-
请在 catch 块中打印 stackTrace 并查看异常抛出了哪一行。
-
另一边工作正常 - 是吗?你能检查一下吗?
-
这里抛出异常:Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);这是例外:现有连接被远程主机强行关闭
-
ckoenig - 你的意思是在服务器端吗?
标签: c# sockets asynchronous udp