简体   繁体   中英

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:

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. 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".

Or, you can set a socket use protocol Icmp, like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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