3

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!

eddie
  • 1,252
  • 3
  • 15
  • 20
Dima Daron
  • 548
  • 1
  • 7
  • 21

3 Answers3

0

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.

Community
  • 1
  • 1
Christopher Stevenson
  • 2,843
  • 20
  • 25
  • This is not entirely true, if port is unrechable, icmp packet is sent from destination with "unrechable port" message. I know for a fact that this udp port is unrechable. I checked it with wire shark and there is a icmp packet sent from destination to me, but for some reason I can't catch it. – Dima Daron Apr 23 '13 at 07:46
  • But thanks for the link, it solved me a "How to wait for response" question i was lookig answer for :) – Dima Daron Apr 23 '13 at 08:06
  • maybe [this](http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/f8d9b559-fa29-4596-b6db-35f455fd88f8/) will help? – Christopher Stevenson Apr 23 '13 at 13:36
  • Here's another [SO question](http://stackoverflow.com/questions/4097165/icmp-listener-problems). – Christopher Stevenson Apr 24 '13 at 19:21
  • Thanks,I tried that and i still didn't catch that packet. I think it's a sync issue. Packet is gone when i'm trying to catch it. – Dima Daron Apr 24 '13 at 20:30
0

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

iou90
  • 257
  • 3
  • 9
0

The address seems to be the issue i tried it and if you change it for 127.0.0.1 you do receive the icmp message

user2696482
  • 63
  • 2
  • 8