ping IP address

G

Guest

Hi all

I need a simple program that allows me to check if an IP address is
pingable. I am not going to send/receive anything to the remote host, just
check if it is visible.
Something like this:

private bool CheckIPAddress(string address)
{
if (Ping(address))
MessageBox.Show("Ping success!");
else
MessageBox.Show("Timeout");
}

private bool Ping(string address)
{
//???
}

Thank you
 
P

Peter Duniho

I need a simple program that allows me to check if an IP address is
pingable. I am not going to send/receive anything to the remote host,
just check if it is visible.

You can't really find that out without sending or receiving anything. By
definition, a "ping" involves sending some data and waiting for some other
data to come back.

You could try to figure out whether the IP address is resolvable. That
is, whether it's associated with a known Internet connection. The
System.Net.Dns class is useful for that sort of thing (use
GetHostByAddress method). But that won't tell you whether there's an
actual computer connected to the IP address or not. It's not what most
people think of when they talk about an address being "pingable".

On the other hand, you can try to figure out whether there's an actual
computer connected to the IP address or not. But to do that you have to
try to communicate with the computer. Even if there is a computer
attached, it may or may not respond to your request. However, assuming it
does, the most common means of doing what you want is to actually "ping"
the computer. In .NET, you can do this using the
System.Net.NetworkInformation.Ping class:

http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

Hope that helps.

Pete
 
G

Guest

Thank you Peter

Yes this is exactly what I want but this Ping method is specific to NET 3.0,
and I am working in NET 1.1... ;(
 
P

Peter Duniho

Thank you Peter

Yes this is exactly what I want but this Ping method is specific to NET
3.0, and I am working in NET 1.1... ;(

Well, technically it's new to .NET 2.0. However, I geuss that still
doesn't solve your problem. :)

But it does mean that what you want to do can't really be done in .NET
very easily. I believe that using raw sockets you can create the
necessary ICMP datagram and "ping" an IP address that way. Doing so will
mean you need to read the ICMP specification and learn how to construct a
correct datagram and interpret the response. Nothing terribly tricky in
there, but it will require extra effort on your part.

Alternatively, a very kludgy but possibly suitable solution (depending on
your exact needs) would be to take advantage of the command prompt "ping"
command, by using it in an external process for which you've capture the
output (you can parse the output and see the results of the ping).

Personally, if it were me I'd explain to "whom it may concern" that the
project either upgrades to .NET 2.0 or doesn't get a "ping" feature. I
understand not everyone is in a position to make those kinds of demands
though. You have my sympathy, even if not my assistance. :)

Pete
 
G

Guest

Alex said:
Yes this is exactly what I want but this Ping method is specific to NET 3.0,
and I am working in NET 1.1... ;(

The Ping class requires .NET 2.0 or newer.

In .NET 1.1 you can call Win32 API Icmp functions. I am
attaching a code example below.

Arne

=============================================================

using System;
using System.Net;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct ICMP_ECHO_REPLY
{
public uint Address;
public uint Status;
public uint RoundTripTime;
public ushort DataSize;
public ushort Reserved;
public IntPtr Data;
public IP_OPTION_INFORMATION Options;
}

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct IP_OPTION_INFORMATION
{
public byte TTL;
public byte TOS;
public byte Flags;
public byte OptionsSize;
public IntPtr OptionsData;
public int RealOptionData;
}

public class Icmp
{
public const int IP_SUCCESS = 0;
public const int IP_BUF_TOO_SMALL = 11001;
public const int IP_REQ_TIMED_OUT = 11010;
[DllImport("icmp.dll")]
public static extern IntPtr IcmpCreateFile();
[DllImport("icmp.dll")]
public static extern uint IcmpSendEcho(IntPtr icmpHandle, uint
ipAddr, ref int requestData, ushort requestSize, IntPtr optionInfo, ref
ICMP_ECHO_REPLY replyBuffer, uint replySize, int timeout);
[DllImport("icmp.dll")]
public static extern bool IcmpCloseHandle(IntPtr icmpHandle);
public static bool Ping(string host)
{
uint addr =
BitConverter.ToUInt32(IPAddress.Parse(host).GetAddressBytes(), 0);
IntPtr h = IcmpCreateFile();
int req = 123456789;
ICMP_ECHO_REPLY rep = new ICMP_ECHO_REPLY();
uint retval = IcmpSendEcho(h, addr, ref req, 4, IntPtr.Zero,
ref rep, 32, 10);
IcmpCloseHandle(h);
return (retval != 0 && rep.Status == IP_SUCCESS);
}
}

public class TestClass
{
public static void Main(string[] args)
{
Console.WriteLine("min server : " + Icmp.Ping("192.168.1.10"));
Console.WriteLine("ikke eksisterende : " +
Icmp.Ping("192.168.1.25"));
Console.WriteLine("www.google.dk (svarer) : " +
Icmp.Ping("216.239.59.104"));
Console.WriteLine("www.eksperten.dk (svarer ikke) : " +
Icmp.Ping("62.199.138.148"));
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top