connect() to socket TIMEOUT?

connect() to socket TIMEOUT?

Post by Harland Mac » Fri, 14 May 1993 02:18:24



Is there a way to define the timeout for connect()?

What I got :
  - I have 2 machines
  - I have the server and client programs written and working  
  - If both machines are up and my server isn't up - connect() returns an
    error right away.

My Problem :
  - the machine is down/off I want to connect to
  - the client (program trying to connect) waits at the connect() command for
    about 30 seconds before returning an error.

I can't wait 30 seconds.

If there is not a timeout for the connect is there something else I can do?

Hardy

 
 
 

connect() to socket TIMEOUT?

Post by Tahir Kayan » Fri, 14 May 1993 07:20:45


Use select() to make sure that the socket fd you are giving to connect
is ready for writing. This way you can set the timeout for select as you
like.

 
 
 

connect() to socket TIMEOUT?

Post by Kevin Wal » Fri, 14 May 1993 07:18:58



Quote:> Is there a way to define the timeout for connect()?

> What I got :
>   - I have 2 machines
>   - I have the server and client programs written and working  
>   - If both machines are up and my server isn't up - connect() returns an
>     error right away.

> My Problem :
>   - the machine is down/off I want to connect to
>   - the client (program trying to connect) waits at the connect() command for
>     about 30 seconds before returning an error.

> I can't wait 30 seconds.

> If there is not a timeout for the connect is there something else I can do?

You could stick the connect() around an alarm wrapper:

        alarm(10);
        if (connect (s,&sin,sizeof(struct sockaddr_in)) == -1){
                if (errno == EINTR)
                        printf("oops, timeout\n");
                else{
                        perror("connect");
                        exit(1);
                }
        }
        alarm(0);

It would also help to have the SIGALRM signal caught and some
variable set to verify that it was the SIGALRM that caused the
system call to be interrupted.

--
   _/   _/  _/_/_/_/  _/    _/  _/_/_/  _/    _/
  _/_/_/   _/_/      _/    _/    _/    _/_/  _/     Professor Kevin Walsh

_/   _/  _/_/_/_/      _/    _/_/_/  _/    _/

 
 
 

connect() to socket TIMEOUT?

Post by David McN » Sat, 15 May 1993 07:40:18




|> Is there a way to define the timeout for connect()?
|>
|> What I got :
|>   - I have 2 machines
|>   - I have the server and client programs written and working  
|>   - If both machines are up and my server isn't up - connect() returns an
|>     error right away.
|>
|> My Problem :
|>   - the machine is down/off I want to connect to
|>   - the client (program trying to connect) waits at the connect() command for
|>     about 30 seconds before returning an error.
|>
|> I can't wait 30 seconds.
|>
|> If there is not a timeout for the connect is there something else I can do?

There's no way that I know of that you can change the
timeout value (at least not for a specific socket).
But you can do something like:

  fcntl(socket, F_SETFL, FNDELAY);     /* make this socket's calls non-blocking */

  if (connect(socket, <address>, <address size>) == -1 &&
      errno != EINPROGRESS) {

      /*
       * If non-blocking connect() couldn't finish, it returns
       * EINPROGRESS.  That's OK, we'll take care of it a little
       * later, in the select().  But if some other error code was
       * returned there's a real problem...
       */

      <handle the problem>

  } else {

    struct timeval tval;

    tval.tv_sec = 5;     /* timeout in 5 seconds... */
    tval.tv_usec = 0;    /* ...plus 0 microseconds */

    switch (select(<on "socket" (four arguments)>, &tval)) {

      /*
       * select() will return when the socket is ready for action,
       * or when there is an error, or the when timeout specified
       * using tval is exceeded without anything becoming ready.
       */

      case 0:     /* timeout */
        <do whatever you do when you couldn't connect>

      case -1:    /* error */
        <do whatever you do if something bad happens>

      default:    /* your file descriptor is ready... */
        <do whatever you do when make a connection>
    }
  }

That's the general idea, anyway.

From this and your previous question it sounds like you're
writing a client/server application (duh!).  You should really
read Richard Stevens' "UNIX Network Programming".  (Really!)

David
--------
David McNab
Parallel Systems Support
NAS (CSC Contract), NASA/Ames

 
 
 

1. adjust timeout of the socket function connect()?

For the following socket function:

rc = connect(servfd, (struct sockaddr *)&servsockaddr, sizeof(servsockaddr));

rc < 0 if the remote address refuses connection or operation times
out.  As you know, the return of rc value takes virtually 0 seconds
for a refused connection but takes as long as 60-70s before operation
timed out.  Is there a way to cut down this timeout period?  Excuse
me if this is too naive a question.

Regards,    -Bill
--
http://www.globalserve.net/~billz

2. Beowulf

3. Monitor socket-connected program for inactivity timeouts

4. Strange dhcp behavior?

5. TIMEOUT for socket streams connection-oriented "connect"?

6. Modem Lights

7. Sockets Connect Timeout

8. Multiple SCSI controllers

9. Socket "connect" timeout?

10. Socket connect timeout

11. Socket connect timeout with alarm()

12. How to change TCP/IP socket CONNECT timeout on SCO Unix?

13. socket timeout? (socket programming question)