Solved

Pinging in C or C++ How to do it

Posted on 2003-11-20
7
518 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
7 Comments
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
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
Comment Utility
0
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
What language do you use?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Accepted Solution

by:
GloomyFriar earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
EvenOdd challenge 10 81
countAbc challenge 9 49
What is Python programming? 3 62
java  and programming certification ? 4 63
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now