?
Solved

What is the best way to do an "online check"?

Posted on 2003-03-25
8
Medium Priority
?
197 Views
Last Modified: 2011-04-14
Hi all,

What is the best way to check if the system is "online"?  Currently my program is pinging a website, it seems to work most of the time but if the website is down, my program thinks that the system is "offline".  I imagine that there has got to be a better way to do this.  Any help will be appreciated.

Thanks,
Steve
0
Comment
Question by:shagino
[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
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8203280
That strongly depends on the OS. For Win32, you could use 'InternetGetConnectedState()'.
0
 

Author Comment

by:shagino
ID: 8203364
Oops, forgot to include this.  My OS is Linux.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8204022
Normally you want to check if a system is up because you want to interact with that system in some way, for example by sending a request to a specific port etc.

In such a situation you don't really want to test if the system is up, you want to test if that specific service is up and the best way to do that is simply to send a request to that service. For example if you want to check if the web server is up, do a connect() to port 80 on the machine!

If your connect doesn't timeout and get a connection then not only do you know it is up but it will also typically send you some identification of what version it is running etc.

This is a lot more reliable than pinging the machine, since even if ping is up it doesn't have to mean that the web server is up.

If you don't know which service you might want to request from it but still just want to know if it is 'up' oor not, then 'ping' is the generic way to test for it, but that answer isn't really what you want and is therefore not really reliable.

Alf
0
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!

 

Author Comment

by:shagino
ID: 8204171
My program is basically just trying to send out email every so often.  But, before mail is sent out, my program does an online check to see if it has to connect before sending out mail.  Should I connect() to the ISP to check if I'm on online?  Thanks for your help.

Steve
0
 
LVL 12

Expert Comment

by:Salte
ID: 8204225
No, just try to connect() to the mail server on the SMTP port, if it doesn't work you're not on-line if it works you're online and you can send the mail.

Alf
0
 

Author Comment

by:shagino
ID: 8204306
Sounds like that's exactly what I need to do.  I feel really stupid for asking this but how do I connect to the mail server on the SMTP port?

Thanks,
Steve
0
 

Author Comment

by:shagino
ID: 8204335
Sounds like that's exactly what I need to do.  I feel really stupid for asking this but how do I connect to the mail server on the SMTP port?

Thanks,
Steve
0
 
LVL 12

Accepted Solution

by:
Salte earned 200 total points
ID: 8204485
Err...just do a connect() and specify the SMTP port when you connect.

To be specific:

step 1. Create socket:
int sock = socket(AF_INET, SOCK_STREAM, 0);

step 2. Get the IP number of the host you want to connect to. (This step can be omitted if you already know the IP number).

struct hostent * he = gethostbyname("smtp.foobar.com");

step 3. build a sockaddr_in structure:

struct sockaddr_in sin;

sin.sin_family = AF_INET;
memcpy(& sin.sin_addr, he -> h_addr, sizeof(sin.sin_addr));
sin.sin_port = htons(25); // SMTP port.

connect(sock,& sin, sizeof(sin));

do a shutdown() and then close() on the socket to close it when you're done. Use recv() to receive data from the server and use send() to send data to the server.

Alf
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

800 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