Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

ICMP using raw socket not working on Vista

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gavin - 07 Sep 2007 11:12 GMT
Hello

I have some code (see below) that sends an ICMP echo request and that works
fine on Windows XP and 2003 but fails on Vista. The code sends the echo
request but times out trying to receive the echo reply. If I use MS network
monitor I can see the echo request being sent and the echo reply being
returned but my app doesn't receive it.

I've turned off the Vista firewall and have confirmed that using ping.exe to
the same address works. Also, if I use the .NET Ping class this also works.
So, there must be something wrong with my code although, as I said, this
works fine on the other OS's.

My code is below. A SocketException is thrown in the ReceiveFrom.

How do I fix this code so it works on Vista too?

Thanks
Gavin

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace IcmpTest {
   class Program {
       // ICMP Echo request
       static readonly byte[] bytesToSend = {
           0x08, 0x00, 0x28, 0x01, 0xde, 0xfc, 0x00, 0x01,
           0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
           0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
           0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
           0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
       };

       static void Main(string[] args) {
           try {
               Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.Icmp);
               socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 5000);
               socket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.IpTimeToLive, 32);

               socket.SendTo(bytesToSend, (EndPoint)new
IPEndPoint(IPAddress.Parse("64.26.22.64"), 0));

               byte[] responseBytes = new byte[1024];

               EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
               int bytesReceivedCount = socket.ReceiveFrom(responseBytes,
ref remoteEndPoint);

               Console.WriteLine(bytesReceivedCount);
           }

           catch (Exception ex) {
               // ReceiveFrom throws a SocketException -
               // A connection attempt failed because the connected party
did not properly respond
               // after a period of time, or established connection failed
because connected host has failed to respond
               Console.WriteLine(ex.ToString());
           }
       }
   }
}
Vadym Stetsiak - 07 Sep 2007 15:09 GMT
Hello, Gavin!

.NET ping class uses Win32 IP Helper Functions from Iphlpapi.dll.
Internally, it can use something other then sockets...

Did you try to specify ProtocolType.Ip when creating socket?
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
ProtocolType.Ip);

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote  on Fri, 7 Sep 2007 03:12:04 -0700:

G> Hello

G> I have some code (see below) that sends an ICMP echo request and that
G> works  fine on Windows XP and 2003 but fails on Vista. The code sends
G> the echo  request but times out trying to receive the echo reply. If
G> I use MS network  monitor I can see the echo request being sent and
G> the echo reply being  returned but my app doesn't receive it.

G> I've turned off the Vista firewall and have confirmed that using
G> ping.exe to  the same address works. Also, if I use the .NET Ping
G> class this also works.
G> So, there must be something wrong with my code although, as I said,
G> this  works fine on the other OS's.

G> My code is below. A SocketException is thrown in the ReceiveFrom.

G> How do I fix this code so it works on Vista too?

G> Thanks
G> Gavin

G> using System;
G> using System.Collections.Generic;
G> using System.Net;
G> using System.Net.Sockets;
G> using System.Text;

G> namespace IcmpTest {
G>     class Program {
G>         // ICMP Echo request         static readonly byte[]
G> bytesToSend = {
G>             0x08, 0x00, 0x28, 0x01, 0xde, 0xfc, 0x00, 0x01,
G> 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,             0x08,
G> 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,             0x10, 0x11,
G> 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,             0x18, 0x19, 0x1a,
G> 0x1b, 0x1c, 0x1d, 0x1e, 0x1f         };

G>         static void Main(string[] args) {
G>             try {
G>                 Socket socket = new
G> Socket(AddressFamily.InterNetwork,
G> SocketType.Raw, ProtocolType.Icmp);
G>                 socket.SetSocketOption(SocketOptionLevel.Socket,
G> SocketOptionName.ReceiveTimeout, 5000);
G>                 socket.SetSocketOption(SocketOptionLevel.IP,
G> SocketOptionName.IpTimeToLive, 32);

G>                 socket.SendTo(bytesToSend, (EndPoint)new
G> IPEndPoint(IPAddress.Parse("64.26.22.64"), 0));

G>                 byte[] responseBytes = new byte[1024];

G>                 EndPoint remoteEndPoint = new
G> IPEndPoint(IPAddress.Any, 0);
G>                 int bytesReceivedCount =
G> socket.ReceiveFrom(responseBytes,  ref remoteEndPoint);

G>                 Console.WriteLine(bytesReceivedCount);
G>             }

G>             catch (Exception ex) {
G>                 // ReceiveFrom throws a SocketException -
G>                 // A connection attempt failed because the connected
G> party  did not properly respond                  // after a period of
G> time, or established connection failed  because connected host has
G> failed to respond
G>                 Console.WriteLine(ex.ToString());
G>             }
G>         }
G>     }
G> }
Gavin - 08 Sep 2007 00:18 GMT
Hello Vadym

I need to use ProtocolType.Icmp as I need access to the IP headers.

Regards
Gavin

> Hello, Gavin!
>
[quoted text clipped - 82 lines]
>  G>     }
>  G> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.