Take a look here for more info (it's part of Beej's guide to network programming, which is a recommended read !) :
http://www.beej.us/guide/b
Main Topics
Browse All TopicsI have written client program in C language using unix. It is talking to server program.
I want to set up a time out for Client. For example, after a fixed amount of time, if it does not get a response from sever, then it times out.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Take a look here for more info (it's part of Beej's guide to network programming, which is a recommended read !) :
http://www.beej.us/guide/b
fprintf(perr, "\nSending the message.... %s", msg); /* send the message */
if( send(sock_id, msg, sizeof(msg), 0) < 0 ) {
fprintf(perr, "\nFailed to send data to widget#2.");
fclose(perr);
exit(5);
}
????? BETWEEN SEND AND RECV Function I want to set up TIME- OUT ???
/* Receive the echoed message from the Widget#2 */
if( recv(sock_id, msg, sizeof(msg), 0) < 0 ) {
fprintf(perr, "\nFailed to receive data from widget#2.");
fclose(perr);
exit(6);
}
printf("The message from the server was: %s\n", msg);
>????? BETWEEN SEND AND RECV Function I want to set up TIME- OUT ???
Time out implies that there is an operation that times out. What operation are you performing between send and recv?
If you meant simply wait for a specific period of time, then use either nanosleep() in case you want to sleep while waiting or alarm() in case you wish to work on something else while waiting.
If you actually meant recv timeout then use select as has already been suggested.
Another option for timing out is to use setsockopt() with SO_RCVTIMEO option. Check man page on your system to verify if this option has been implemented in your platform.
http://linux.die.
I have created the socket ( sock_id)......
Now after this and before connect() ( see below ..connect, send and recv ) I want to set up time out for 30 seconds for example..
>>>>>>>>>
??? Set up time out here
SAY ..If I don't receive anything from the server which I am checking in the last block, then I should be message: timed out.. ??????
if( connect(sock_id, (struct sockaddr *)&client, sizeof(client)) < 0) {
fprintf(perr, "Error: Failed to connect to widget#2.\n");
fclose(perr);
exit(4);
}
else
fprintf(perr, "Connection to widget#2 successful.\n");
fprintf(perr, "Sending the message.... %s\n", msg); /* send the message */
if( send(sock_id, msg, sizeof(msg), 0) < 0 ) {
fprintf(perr, "Error: Failed to send data to widget#2.\n");
fclose(perr);
exit(5);
}
/* Receive the echoed message from the Widget#2 */
if( recv(sock_id, msg, sizeof(msg), 0) < 0 ) {
fprintf(perr, "Error: Failed to receive data from widget#2.\n");
fclose(perr);
exit(6);
}
printf("The message from the server was: %s\n", msg);
>>>>>
Business Accounts
Answer for Membership
by: Infinity08Posted on 2008-07-07 at 11:02:24ID: 21947218
You can specify a timeout with select :
/select
http://linux.die.net/man/2