Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check if a web service is running?

Status
Not open for further replies.

JustFillingIn

Programmer
May 19, 2011
3
0
0
US
I am calling a web service using a socket call. I want to prevent my client app from 'hanging' until the socket call times out if the service is not running. The app is working fine if the service is running.

//Create the Socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)
{
printf("errno %d: %s\n",errno,strerror(errno));
set_output_data(in_out_address->error,
"E991",
ERROR_SIZE);
return;
};
printf("errno %d: %s\n",errno,strerror(errno));
printf("sockfd %d: %s\n",sockfd,"socket call");

//Connect
errno=0;
connresult = connect(sockfd,(struct sockaddr *)&server_addr,
sizeof(server_addr));
if (connresult < 0)
{
printf("errno %d: %s\n",errno,strerror(errno));
set_output_data(in_out_address->error,
"E992",
ERROR_SIZE);
return;
}
printf("errno %d: %s\n",errno,strerror(errno));
printf("connresult %d: %s\n",connresult,"connection call");

// check if service is running before we send data

 
Is the web service running on your machine or on the remote machine
 
Thank you Dian. I receieved a compile error when I used the suggested code, so there may be a header file that I am missing. Here are the ones I am using.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <stdbool.h>
#include <errno.h>

I tried to just use the setsockopt command without the if to set the timeout, and the program compiled fine but returned an error code at runtime indicating an unsupported function. I think for now, we are just going to let the client application timeout on its own if the service is not running since it is rarely the situation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top