Solved

Pinging in C or C++ How to do it

Posted on 2003-11-20
7
520 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

815 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

13 Experts available now in Live!

Get 1:1 Help Now