Thread: ping an IP and get status

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16

    ping an IP and get status

    Dear friends,
    i want to make a C program which can ping an IP and provide the status whether that device is available or not in the form of some integer value i.e for available give '1' and other for NULL

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean like what the "ping" program does?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to Networking/Device Communication.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    You may want to consider the ICMP API if you're on the Windows platform.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by krishna View Post
    Dear friends,
    i want to make a C program which can ping an IP and provide the status whether that device is available or not in the form of some integer value i.e for available give '1' and other for NULL
    First thing to realize, is that just because a device does not respond to an ICMP "ping", does not mean that device is unavailable. It just might not be responding to ping, for many legitimate reasons.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    First thing to realize, is that just because a device does not respond to an ICMP "ping", does not mean that device is unavailable. It just might not be responding to ping, for many legitimate reasons.
    Not to mention that if the device isn't connected directly to the computer running such a PING operation, it can fail at any level in between, and the device is in itself perfectly operational, just that some segment of network in between failed [or didn't forward the PING or PING-REPLY message] for some reason.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    RFC 792 provides a lot of info on the ICMP messages including a detailed breakdown of destination unreachable codes.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    42
    ok. so how do you make a program that will ping an address, regardless if the results are positive or negative?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Seriously, what are you trying to do?

    You "make" the program by reading the RFC, writing code to send out the right kind of message, then interpreting the responses (or lack of).

    If you don't know how to do that, then looking at an existing "ping.c" program might help. But the good ones (with a reasonable level of functionality) are not exactly small.

    Or if you just don't want to know about the code, just write a simple wrapper around the "ping" program which comes with your OS.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed