Csharp/C#教程:远程主机强制关闭现有连接分享


远程主机强制关闭现有连接

我需要从异步套接字服务器获取UDP数据报,但我的应用程序中发生exception:

出现问题:

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:usersuserdocumentsvisual studio 2010ProjectsConsoleApplication7ConsoleApplication7Program.cs 

可能是什么问题?


为了提供更多信息,我在这个页面上购买了私人袜子连接: http : //rapidsocks.com/这个服务给我一个IP和端口列表谁真的不是代理..只是给我一个代理的连接IP :来自服务器上的池的proxyPort响应…

如何从服务器上获取proxyIP:proxyPort的答案?

这确实是一个通用的错误消息,可能意味着什么。 是时候让低级网络流量嗅探器过滤实际出错的地方了。 添加额外的error handling尝试使用正确的日志记录服务器上的catch块始终是一个很好的起点。

在UDP域中,可能出现的一种方法是将UDP数据包发送到主机,并且远程主机在该端口上没有侦听器,并在响应中弹回ICMP主机不可达消息。

https://support.microsoft.com/kb/263823/en-us

此exception告诉您没有进程正在侦听该端口。


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

上述就是C#学习教程:远程主机强制关闭现有连接分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/958369.html

(0)
上一篇 2021年11月22日
下一篇 2021年11月22日

精彩推荐