LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2011, 03:49 AM   #1
pops13
LQ Newbie
 
Registered: Aug 2009
Posts: 11

Rep: Reputation: 0
Setting timeout time for TCP sockets.


Hi,

I have written simple TCP server-client programs in 'C' on my RHEL 5 64 bit.
I want to add SEND and RECEIVE timeout for this socket; so that if server is not responding in given time, request will get timeout instead of waiting in "recv" call.
But I failed to do so. can anybody help around it OR can redirect me to some link that will explain this.

Thanks in advanced.
 
Old 06-08-2011, 05:41 AM   #2
NM04
Member
 
Registered: Jan 2011
Distribution: Back Track,Fedora,centos
Posts: 240

Rep: Reputation: 14
hi pops,

The easiest is to setup an alarm and have connect be interrupted with a signal (see UNP 14.2):


signal( SIGALRM, connect_alarm ); /* connect_alarm is you signal handler */
alarm( secs ); /* secs is your timeout in seconds */
if ( connect( fs, addr, addrlen ) < 0 )
{
if ( errno == EINTR ) /* timeout */
...
}
alarm( 0 ); /* cancel alarm */

regards,
nm04
 
Old 06-08-2011, 11:01 AM   #3
pops13
LQ Newbie
 
Registered: Aug 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks NM for responding with solution...

But this you tell about connect;
1) Is this applicable to "recv" and "send" system calls as well.
2) Is there any other way, other than signal handling; like we dose in case of normal sockets as follows,

Quote:
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

if (setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
error("setsockopt failed\n");
 
Old 06-09-2011, 05:19 AM   #4
NM04
Member
 
Registered: Jan 2011
Distribution: Back Track,Fedora,centos
Posts: 240

Rep: Reputation: 14
pops,

it should be applicable to send & recv functions i have not checked, but one thing if you apply timeout it will not connect then how will it proceed towards send & recv functions ?
 
Old 06-10-2011, 01:34 AM   #5
pops13
LQ Newbie
 
Registered: Aug 2009
Posts: 11

Original Poster
Rep: Reputation: 0
NM,

I need to set timeout to "send" and "recv" only, not to "connect".

So after connection, if server is not responding within timeout period, client must not wait longer than timeout period.
Dose this makes sense?

Thanks,
Pops.
 
Old 06-10-2011, 07:07 AM   #6
NM04
Member
 
Registered: Jan 2011
Distribution: Back Track,Fedora,centos
Posts: 240

Rep: Reputation: 14
yes that does make sense, I told you that alarm should work on send and recv functions.
Code:
There are three ways to place a timeout on an I/O operation involving a socket.

1. Call alarm, which generates the SIGALRM signal when the specified time has 
expired. This involves signal handling, which can differ from one implementation
to the next, and it may interfere with other existing calls to alarm in the process.

2. Block waiting for I/O in select, which has a time limit built in, instead of blocking
in a call to read or write.

3. Use the newer SO_RCVTIMEO and SO_SNDTIME) socket options. The problem
with this approach is that not all implementations support these two socket options.
Each is appropriate, depending on the specific circumstances. I hope this helps.

kind regards,
 
Old 06-10-2011, 09:31 AM   #7
pops13
LQ Newbie
 
Registered: Aug 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks NM...
that will really help.

Let me work a little on option 2 you provided as well.
Quote:
2. Block waiting for I/O in select, which has a time limit built in, instead of blocking
in a call to read or write.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming timeout with sockets simasimon Programming 6 05-03-2008 12:16 AM
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
TCP Network Sockets Rakoon Red Hat 1 08-04-2005 12:36 AM
Configuring timeout for sockets hairysocks Linux - Networking 0 04-16-2004 07:07 AM
Maximum TCP-Sockets Quis Linux - Networking 2 01-17-2003 03:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration