Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pinging in C or C++ How to do it

Posted on 2003-11-20
7
Medium Priority
?
526 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:acbirdie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 9789035
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>

main()
{
  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\n",str);
    scanf(fp,"%s\n",str);//you're only interested in the actual ping part
    if (strcmp(str,str2) || strcmp(str,str3))
      printf("Error");
    else
      connect();
      fclose(fp);
  }
}

Or something like that. It's not elegant, but I think it may work.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9789614
What language do you use?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Accepted Solution

by:
GloomyFriar earned 1500 total points
ID: 9789639
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.
0
 
LVL 22

Expert Comment

by:cookre
ID: 9790241
gethostbyaddr() will verify the IP a bit faster than a ping:

gethostbyaddr((const char *) NBO,LengthOfAddr,AF_INET);
0
 
LVL 1

Expert Comment

by:Skytzo
ID: 9795112

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.
http://www.developerweb.net/sock-faq/

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.

??
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 9795906
Skytzo
>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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Make the most of your online learning experience.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question