我需要从异步套接字服务器获取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
有什么问题吗?
为了提供更多的信息,我在这个页面上购买了私人socks连接:http://rapidsocks.com/这个服务给我一个IP和端口列表,他们实际上不是代理。只是一个连接给我一个proxyIP:proxyPort从服务器上的一个池响应.
如何从服务器获得proxyIP:proxyPort的答案?
发布于 2011-08-26 08:36:10
这确实是一条通用错误消息,可能意味着任何事情。是时候让低级别的网络流量嗅探器来过滤到底出了什么问题。在服务器上添加额外的错误处理、尝试捕获块和良好的日志记录始终是一个很好的起点。
发布于 2011-09-19 23:32:05
在UDP中,出现这种情况的一种方式是向主机发送UDP数据包,而远程主机在该端口上没有侦听器,并在响应中弹出ICMP主机不可访问的消息。
在简单的英语中,这个异常告诉您没有进程在该端口的远端侦听。
Update:您应该能够使用以下代码避免这种行为:
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);
微软的文章263823说到这个主题:2019年很难找到。
症状在Windows2000中,用户数据报协议(UDP)程序可能无法工作,并可能生成WSAECONNRESET响应。
因为如果使用sendto函数发送数据报会导致"ICMP端口不可访问“响应,并且select函数被设置为readfds,则程序返回1,随后对recvfrom函数的调用不适用于WSAECONNRESET (10054)错误响应。在MicrosoftWindowsNT4.0中,这种情况会导致select函数阻塞或超时。
解决方案一种名为"SIO_UDP_CONNRESET“的新套接字已在Windows 2000中引入。当使用此IOCTL时,必须为Windows 2000专门重写程序,以获得原始的WindowsNT4.0行为。Windows NT 4.0、Microsoft Windows 95和Microsoft Windows 98不支持此新的IOCTL。除了重写应用程序之外,您还需要本文中进一步引用的修补程序。
https://stackoverflow.com/questions/7201862
复制相似问题