LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2007, 10:56 AM   #1
ed.poore
LQ Newbie
 
Registered: Jan 2006
Location: United Kingdom
Posts: 3

Rep: Reputation: 0
Question Socket reuse


Ok, a bit of background I think would be useful to explain my problem. I'm doing some research work for the company I work for, basically it's creating a web-interface for one of their electronic systems. The idea will ultimately be to run the entire thing as an embedded system (this point will hopefully justify my decisions later on).

Now, the system requires for safety reasons that a serial link is always maintained between the controlling unit (my PC in this case) and the system. So I've been writing a daemon-like program which deals with this. Because a new copy of the CGI program is run everytime a connection is made through Apache that program can't deal with the serial port control, so there's a socket connecting the daemon to the CGI program which issues the commands and receives the acknowledgements.

So far all has been going well but if the socket is reused more than twice in quick succession then it bombs out because the socket hasn't timed out yet (in the kernel I presume). What are my options, basically there aren't going to be that many simultaneous connections, if any at all (none during the research phase anyway) but there will be lots of consecutive connections due to the CGI program being executed for each request made.

So basically is there a way of telling the kernel to keep the socket open all the time or have an instantaneous timeout on the socket? Or will I have to recreate the socket after a client has disconnected?

PS, I don't particularly want to use Unix domain sockets because using the TCP will give me flexibility in the future, which I will need when my boss suggests something I'm pretty sure he's going to suggest at some point .
 
Old 02-14-2007, 01:23 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. If you're not already doing so, it sounds like the ideal approach is to use UDP datagrams (and "sendto()" and "recvfrom()").

2. It sounds like the problem you're running into might be TCP/IP's "TIME_WAIT" state. Here's a bit of an explanation about it:

http://www.developerweb.net/forum/showthread.php?t=2941

3. And here's an excellent article regarding this (and similar) issues:
http://www-128.ibm.com/developerwork...pit/index.html

4. You can tune your OS (by either/both:
a) decrease the TIME_WAIT
b) increase the #/ephemeral ports

I would definitely choose "b)" (increase the #/ephemeral ports) before "a)" (decrease time wait).

You can tune both parameters on Linux as well as Windows:

Quote:
Linux:
-----
cat /proc/sys/net/ipv4/ip_local_port_range
32768 61000
cat /proc/sys/net/ipv4/tcp_fin_timeout
60
<= These are reasonable values
Quote:
Windows:
-------
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters
TcpTimedWaitDelay= 30 (decimal)
MaxUserPort= 65534 (dec)
KeepAliveInterval= 1 (dec)

Last edited by paulsm4; 02-14-2007 at 01:35 PM.
 
Old 02-14-2007, 02:19 PM   #3
ed.poore
LQ Newbie
 
Registered: Jan 2006
Location: United Kingdom
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by paulsm4
1. If you're not already doing so, it sounds like the ideal approach is to use UDP datagrams (and "sendto()" and "recvfrom()").
Thanks, I was looking into using UDP instead of TCP but didn't find an example in the book I got on Linux programming (chiefly a Windows developer, or embedded software this is my first serious foray into "low-level" linux programming. I just had another look in the book and there is an example so will take a look once I sort out a few other things (not related to this work).

Quote:
Originally Posted by paulsm4
2. It sounds like the problem you're running into might be TCP/IP's "TIME_WAIT" state. Here's a bit of an explanation about it:http://www.developerweb.net/forum/showthread.php?t=2941
3. And here's an excellent article regarding this (and similar) issues:
http://www-128.ibm.com/developerwork...pit/index.html
4. You can tune your OS (by either/both:
a) decrease the TIME_WAIT
b) increase the #/ephemeral ports
I would definitely choose "b)" (increase the #/ephemeral ports) before "a)" (decrease time wait).
You can tune both parameters on Linux as well as Windows:
Thanks I'll look into all of these options if the UDP doesn't pay off. It was more a question of whether there was a parameter where you could set the timeout for a particular socket.
 
Old 02-14-2007, 04:24 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Why are you closing the socket in he first place? I don't understand the idea fully, it seems. Is the program started during every connection?

If the daemon is just working, you can keep the socket you have accept() on and only open and close the accepted connection. That would not cause the timeout (in fact, will cause a much shorter one).
 
Old 02-14-2007, 04:32 PM   #5
ed.poore
LQ Newbie
 
Registered: Jan 2006
Location: United Kingdom
Posts: 3

Original Poster
Rep: Reputation: 0
I'm not actually closing the socket, I'm doing as you say. I open the socket and accept a connection then close the connection. But this can only happen twice and then the accept fails with "Bad File Descriptor", the code I based this on was from "Linux Application Development" by Addison Wesley and there it has a comment in the code highlighting this fact.
 
Old 02-14-2007, 04:42 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I'd like to see that code, as it should not happen. Please explain one thing...when you get to that error, does it repair itself or not? If it does, what's the time (more or less)?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
reuse lvm partitions after format kpachopoulos Linux - Software 1 02-25-2006 09:26 AM
Ubuntu reuse /home/directory coming from MEPIS/debian? lefty.crupps Ubuntu 3 10-11-2005 08:40 PM
disallow password reuse cambie Linux - Security 0 11-29-2004 01:47 PM
reuse Makefile on different .c? chunlee Linux - Newbie 0 11-06-2004 07:48 PM
Cannot reuse CD-RW after first burn lansman Linux - Newbie 2 11-15-2003 03:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:23 AM.

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