G
Gareth Crispin
Good afternoon,
I'm rewriting a piece of perl code in C++ that simply checks
availability of a port.
It's fairly simple, and returns UP, DOWN or NODNS.
This works great, and is just what I need. Except when network issues,
or firewalls drop packets silently. It'll sit and hang around forever,
which grinds the scripts dependant upon it to a halt.
Is there a way of being able to kill the connect() or set a timeout of
say three seconds before assuming a port is DOWN.
I've heard talk of using SIGALARM to break the connect(), but I've no
idea how to implement this. Would anyone mind pointing me in the right
directions?
I'm not concerned with running on anything other than UNIX.
I'm pretty green when it comes to C, so please be gentle.
Thanks in advance, and kind regards
Gareth Crispin
---- CODE ----
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 13702 // the port client will be connecting to
int main(int argc, char *argv[])
{
int sockfd, numbytes;
struct hostent *he;
struct sockaddr_in their_addr; // connector's
address information
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
//perror("gethostbyname");
printf("NODNS");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short,
network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest
of the struct
if (connect(sockfd, (struct sockaddr *)&their_addr,
sizeof(struct sockaddr)) == -1) {
//perror("connect");
printf("DOWN");
exit(1);
}
printf("UP");
close(sockfd);
return 0;
---- END CODE ----
I'm rewriting a piece of perl code in C++ that simply checks
availability of a port.
It's fairly simple, and returns UP, DOWN or NODNS.
This works great, and is just what I need. Except when network issues,
or firewalls drop packets silently. It'll sit and hang around forever,
which grinds the scripts dependant upon it to a halt.
Is there a way of being able to kill the connect() or set a timeout of
say three seconds before assuming a port is DOWN.
I've heard talk of using SIGALARM to break the connect(), but I've no
idea how to implement this. Would anyone mind pointing me in the right
directions?
I'm not concerned with running on anything other than UNIX.
I'm pretty green when it comes to C, so please be gentle.
Thanks in advance, and kind regards
Gareth Crispin
---- CODE ----
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 13702 // the port client will be connecting to
int main(int argc, char *argv[])
{
int sockfd, numbytes;
struct hostent *he;
struct sockaddr_in their_addr; // connector's
address information
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
//perror("gethostbyname");
printf("NODNS");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(PORT); // short,
network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest
of the struct
if (connect(sockfd, (struct sockaddr *)&their_addr,
sizeof(struct sockaddr)) == -1) {
//perror("connect");
printf("DOWN");
exit(1);
}
printf("UP");
close(sockfd);
return 0;
---- END CODE ----