Solved

Pinging in C or C++ How to do it

Posted on 2003-11-20
7
522 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: 9789584
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9789614
What language do you use?
0
Industry Leaders: 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!

 
LVL 6

Accepted Solution

by:
GloomyFriar earned 500 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

Technology Partners: 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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

738 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