|> 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