How to set the timeout for the client

The scenerio is:

  1. A server listens on a port number
  2. If the server is down and the Client tries to connect
  3. How to set the timeout for the client

Detailed explaination:
In a client server architecture over a TCP/IP, normally the server is started first which waits and listens for the client connection. Here if the client application is started first, it exits saying "Error in connecting to server" i.e it is not able to establish connection with the server application. Now, how to program such that either of the client or server application could be started first. Hint: Some kind of time out is maintained in the client application, sothat it waits for certain time to establish connection with the server.

Is this homework? Looks like that to me...

You will have to take care of this in your programming. Will your client use the 'connect' system call to connect to the server? In that case, if no reply is received from the remote end, you will get -1 returned and errno variable set to some value. This value will change from OS to OS.

Check the man page of connect on your system to know what will be set. Then if the error is that the socket has timed out, then, your code can sleep for a bit and then try again.