Timeout value on Socket Connect

Discussion in 'C++' started by Gareth Crispin, Jun 28, 2005.

  1. 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 ----
     
    Gareth Crispin, Jun 28, 2005
    #1
    1. Advertising

  2. Gareth Crispin

    Jack Klein Guest

    Jack Klein, Jun 29, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ted Burhan

    Socket.Connect() timeout

    Ted Burhan, Dec 19, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,577
    Ted Burhan
    Dec 19, 2003
  2. p.
    Replies:
    4
    Views:
    596
    Giampaolo Rodola'
    Jan 10, 2009
  3. Laszlo Nagy
    Replies:
    1
    Views:
    4,855
    Mark Wooding
    Jan 27, 2009
  4. Jean-Paul Calderone
    Replies:
    0
    Views:
    971
    Jean-Paul Calderone
    Jan 27, 2009
  5. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,290
    Brian Candler
    Oct 6, 2004
Loading...

Share This Page