• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

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

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
shagino
Asked:
shagino
  • 4
  • 3
1 Solution
 
jkrCommented:
That strongly depends on the OS. For Win32, you could use 'InternetGetConnectedState()'.
0
 
shaginoAuthor Commented:
Oops, forgot to include this.  My OS is Linux.
0
 
SalteCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
shaginoAuthor Commented:
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
 
SalteCommented:
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
 
shaginoAuthor Commented:
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
 
shaginoAuthor Commented:
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
 
SalteCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now