473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ping IP address

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

Apr 13 '07 #1
4 13173
On Fri, 13 Apr 2007 16:22:01 -0700, Alex K.
<Al***@discussions.microsoft.comwrote:
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/lib...tion.ping.aspx

Hope that helps.

Pete
Apr 14 '07 #2
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... ;(
"Peter Duniho" wrote:
On Fri, 13 Apr 2007 16:22:01 -0700, Alex K.
<Al***@discussions.microsoft.comwrote:
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/lib...tion.ping.aspx

Hope that helps.

Pete
Apr 14 '07 #3
On Fri, 13 Apr 2007 17:32:00 -0700, Alex K.
<Al***@discussions.microsoft.comwrote:
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
Apr 14 '07 #4
Alex K. wrote:
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).GetAdd ressBytes(), 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"));
}
}
Apr 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: techprot-google04 | last post by:
I've found tons of sample code on pinging - but all start with the host name, resolve the ip address and do the ping. Does anyone have any sample code that does a ping from an ip address alone?...
2
by: Devin | last post by:
I am trying to figure out a way to ping IP address. This is because we assign all of our IP address in house, and when we are assigning a new one, we wish to verify that it is not already in use....
0
by: Ed | last post by:
I've attached some VB.NET code I've hacked together (some taken from MS examples & newsgroup postings) that will perform a ping or IcmpSendEcho using the icmp.dll (see this for more info:...
2
by: Ryan | last post by:
I want to ping every IP on a subnet (255 IP's) and display in a table what IP's are active. I'm currently using a For loop but this takes forever because it pauses to test each Ping. So my guess...
5
by: Deepak | last post by:
I am programing a ping application which pings various centers . I used timer loop and it pings one by one. Now when i finish pinging one center it should wait for the ping_completed function to...
1
by: Krish | last post by:
All, I have an offline application that works online for some data syncronization. For data syncronization I access a webservice. I want to show whether my application is online or not by checking...
5
by: Michael M. | last post by:
I have the following code (listed at bottom of post) that pings a small range of IP address to see which ones are alive. To speed things up a little I am trying to use more than one thread,...
6
by: Dave Marden | last post by:
I currently use this routine in vbscript to ping computers and get the status of ping to determine whether to try to backup a machine, I am trying to change it to work with vb2003.net I am...
6
by: belias | last post by:
So...here we go. I'm having an issue with one computer on a network not being able to ping other computers by name. I've spent the last day searching similar issues and I've tried all the steps...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.