connect( ) timeout

This is a discussion on connect( ) timeout within the Networking/Device Communication forums, part of the General Programming Boards category; How do you go about setting the timeout for the connect( ) function?...

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

    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
    Posts
    5,681
    Bunch of ways. The easist (IMO) is something like the following

    Code:
      FD_ZERO(&set);
      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);
    
      close(sock);
    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
    Posts
    6,231
    >>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:

    Code:
    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))
      {
        return(0);
      }
    
      if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
      {
        return(0);
      }
    
      errno = err;
      return(errno == 0);
    }
    You'd call IsSocketConnected() after doing the select().

    The Windows version looks like so:
    Code:
    int isconnected(SOCKET s, fd_set *rd, fd_set *wr, fd_set *ex)
    {
      WSASetLastError(0);
      if (!FD_ISSET(s, rd) && !FD_ISSET(s, wr))
      {
        return(0);
      }
    
      if (FD_ISSET(s, ex))
      {
        return(0);
      }
    
      return(1);
    }
    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, 04:40 PM
  2. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21