简体   繁体   English

如何在 C# 中接收对 UDP 消息的 ICMP 响应“端口无法访问”

[英]How to receive ICMP response “Port unreachable” to UDP message in C#

I'm trying to receive ICMP response "Port unreachable" to UDP message in C# this is what I'm trying to do:我正在尝试在C#接收对 UDP 消息的 ICMP 响应“端口无法访问”,这就是我想要做的:

IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Parse("192.168.211.133"), 0);
var udpClient = new UdpClient("192.168.211.133", 20);
Byte[] messagebyte = Encoding.Default.GetBytes("hi".ToCharArray());
int s = udpClient.Send(messagebyte, messagebyte.Length);
Byte[] ReceiveBuffer = new Byte[256];
ReceiveBuffer = udpClient.Receive(ref remoteEndpoint); 

but the program is stuck on但程序卡住了

ReceiveBuffer = udpClient.Receive(ref remoteEndpoint);

What am I doing wrong?我究竟做错了什么?

Please help me!请帮我!

The whole point of UDP is that there may not BE a response. UDP 的全部意义在于可能没有响应。 After a certain amount of time has passed, you need to assume that the destination is unreadable.经过一定时间后,您需要假设目的地是不可读的。

Take a look at this other question .看看这个其他问题

if you catch a exception and SocketErrorCode == SocketError.ConnectionReset, that means you receive "Port unreachable".如果您捕获异常并且 SocketErrorCode == SocketError.ConnectionReset,则意味着您收到“无法访问端口”。

Or, you can set a socket use protocol Icmp, like this:或者,您可以设置套接字使用协议 Icmp,如下所示:

Socket icmp = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);

地址似乎是我尝试过的问题,如果您将其更改为 127.0.0.1,您确实会收到 icmp 消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM