Search code examples
c#socketsicmp

C# Raw socket Receiving packets


Currently trying to implement a simple ping program to teach myself about network programming in C# and .NET.

I have managed to initialize a raw socket and correctly build an ICMP echo request packet. When running my program, Wireshark confirms that I am sending an Echo Request to the desired destination, however the remote machine will never send back an echo reply. I have tried sending to multiple machines all with the same result (and each of these machines can be pinged using the Windows ping utility). My code goes like this:

IcmpPacket echoReq = new IcmpPacket;
/*Some code to initialize packet*/
rawSocket.Send(echoReq, destinationIP); //syntax may be wrong, dont have the code infront of me sorry
rawSocket.ReceiveFrom(buffer, remoteEndpoint);

If anyone could suggest any reasons why the remote machines do not send any reply, I'd be very grateful.


Solution

  • It's hard to know for sure from the information in your question. There are just too many things that can go wrong. But here are a few that I would start checking through.

    • The ICMP packet could be incorrectly formatted. I would use wireshark to compare the result of your custom ping packet to that of a known functioning utility to see if there are any differences
    • The destinationIP and remoteEndpoint values could point to different addresses. Seems unlikely but wanted to call it out
    • The IP in question could be simply rejecting the ping request. I would verify with another tools that it is returning pings
    • The firewall could be getting in the way. I would disable it temporarily and then rerun my program to see if that was the cause.