Pinging in C or C++ How to do it

Before I try to connect to to a specific IP address and port I want to make sure the IP address exists on the network.  How do I write super simple code to just do that.  So far all I have been reading is way above my head.  Can somebody help.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I think that the time you would spend pinging an ip, would be about the same as trying to connect. If there's an error connecting, you can handle the error and see if it's there or not. Don't know if you can ping with a simple code other than System("ping ip"); but that doesn't return anything to the program.
There is a way, but it's not very elegant.

#include <stdio.h>
#include <dos.h>
#include <string.h>

  FILE *fp;
  char str[80],str2[80],str3[80];
  System("ping ip > text.txt");
  strcpy(str2,"Request timed out");
  strcpy(str3,"Unable to reach host"); //etc for other errors you might want to check
  if (fp=fopen("text.txt","r"))
    scanf(fp,"%s\n",str);//you're only interested in the actual ping part
    if (strcmp(str,str2) || strcmp(str,str3))

Or something like that. It's not elegant, but I think it may work.
What language do you use?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

By the way the simplest code is just to try to connect.
If the connection failed then something wrong with the ip or port.
But if you are writing a scanner may be it's not good solution.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gethostbyaddr() will verify the IP a bit faster than a ping:

gethostbyaddr((const char *) NBO,LengthOfAddr,AF_INET);

You are not looking for a ping then.  To explain, to ping someone you need to send out an ICMP to the desitination which probably requires more than what you want to do.

If you have read any code on connecting, you can simply try to establish a connection to the server that you want to verify.  If the connection is established, then it exists.  If it fails, then it does not exist.

I just came across a site that has answers to alot of interesting questions, you might want to take a look.

Everyone including myself is assuming that you are developing in C or C++ on a unix like platform.
This requires a little bit of work.  I could give you the code to do this if you want, and if you want to understand it or develop it on your own, then get the book Unix Network Programming by Richard Stevens I believe.

If you are coding in C# or Java, then its really simple.

>If you have read any code on connecting, you can simply try to establish a connection to the server that you want to >verify.  If the connection is established, then it exists.  If it fails, then it does not exist.
This is not necessarily true. The server may be down, or may be refusing connections. The connection may hang, or there could be several other errors connecting that don't necessarily mean that it doesn't exist.
Still, as I said in the beginning, it is much simpler to simply try to connect and catch any error that may happen.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.