Thread: connect( ) timeout

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Ontario, Canada

    connect( ) timeout

    How do you go about setting the timeout for the connect( ) function?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Bunch of ways. The easist (IMO) is something like the following

      FD_SET(sock, &set);
      fcntl(sock, F_SETFL, O_NONBLOCK);
      if ( (ret = connect (sock, (sockaddr *)&addr, sizeof (sockaddr))) == -1)
        if ( errno != EINPROGRESS )
          return 0;
      ret = select(sock+1, NULL, &set, NULL, &timeout);
    Thats a code segment obviously.

    What you are doing is setting the socket to be non blocking, as such connect will immediatly fail but should errno should be set to EINPROGRESS from there you use select to see if you can write to it. Select is always blocking but has a timeout parameter.

    There are also ways using signal() and alarm().

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >>from there you use select to see if you can write to it
    The socket can be writable if the connect works or if it fails, so you need another level of testing to see if you're actually connected or not.

    The following is based on Jon Snader's code from Effective TCP/IP Programming:

    int IsSocketConnected(int fd, fd_set *rd, fd_set *wr)
         * Code taken from Effective TCP/IP Programming, by Jon Snader, p185 
      int     err;
      size_t  len = sizeof(int);
      errno = 0;
      if (!FD_ISSET(fd, rd) && !FD_ISSET(fd, wr))
      if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
      errno = err;
      return(errno == 0);
    You'd call IsSocketConnected() after doing the select().

    The Windows version looks like so:
    int isconnected(SOCKET s, fd_set *rd, fd_set *wr, fd_set *ex)
      if (!FD_ISSET(s, rd) && !FD_ISSET(s, wr))
      if (FD_ISSET(s, ex))
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking connect()?
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-22-2009, 03:40 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. connect() function, strange error
    By Mr_Miguel in forum C Programming
    Replies: 1
    Last Post: 12-12-2006, 06:51 PM
  4. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 09:30 PM
  5. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM