gpt4 book ai didi

c# - 现有连接被远程主机强行关闭

转载 作者:可可西里 更新时间:2023-11-01 03:12:20 26 4
gpt4 key购买 nike

我需要从异步套接字服务器获取 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());

}
}
}

异常(exception):

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

可能是什么问题?


为了提供更多信息,我在此页面上购买了私有(private) socks 连接:http://rapidsocks.com/该服务为我提供了一个 IP 和端口列表,这些 IP 和端口实际上不是代理。只是一个连接,从服务器上的池中给我一个 proxyIP:proxyPort 作为响应...

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

最佳答案

在 UDP 领域,发生这种情况的一种方式是当您向主机发送 UDP 数据包时,远程主机在该端口上没有监听器,并弹回 ICMP 主机不可达消息作为响应。

用简单的英语来说,这个异常告诉您没有进程正在监听该端口的远端。


更新:您应该能够使用以下代码避免该行为:

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

Microsoft 文章 263823 关于这个主题说:[截至 2019 年很难找到]

SYMPTOMS In Windows 2000, a User Datagram Protocol (UDP) program may not work and may generate a WSAECONNRESET response.

CAUSE If sending a datagram using the sendto function results in an "ICMP port unreachable" response and the select function is set for readfds, the program returns 1 and the subsequent call to the recvfrom function does not work with a WSAECONNRESET (10054) error response. In Microsoft Windows NT 4.0, this situation causes the select function to block or time out.

RESOLUTION A new sockets IOCTL called "SIO_UDP_CONNRESET" has been introduced in Windows 2000. When this IOCTL is used, the program must be rewritten specifically for Windows 2000 to obtain the original Windows NT 4.0 behavior. Windows NT 4.0, Microsoft Windows 95, and Microsoft Windows 98 have no support for this new IOCTL. In addition to rewriting your application, you will need the hotfix referenced further down in this article.

关于c# - 现有连接被远程主机强行关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7201862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com