473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Client-side TCP socket receiving "Address already in use" upon connect

Hi,

The reason is that my application does about 16 connects and data
transfers per second, to the same 16 remote hosts. After approx 200 secs
there are 4000 sockets waiting to be garbage collected by the OS. At
this point is seems that connect loops and starts using the same local
addresses it used 4000 connections ago, resulting in an "Address already
in use" exception.

A possible solution to this would be to keep the connection to each
remote host open, but that would require parsing of the received data
into the data items (which are files by the way) sent by the application.

My question is if there are any other methods of solving this? Maybe a
socket option of some sort...

regards
Sep 2 '06 #1
11 6784
Sybren Stuvel wrote:
Tor Erik enlightened us with:
>The reason is that my application does about 16 connects and data
transfers per second, to the same 16 remote hosts. After approx 200
secs there are 4000 sockets waiting to be garbage collected by the
OS.

Which OS are we talking about?
Windows XP
>
>At this point is seems that connect loops and starts using the same
local addresses it used 4000 connections ago, resulting in an
"Address already in use" exception.

After how many seconds does this happen?
200 seconds approx
>
>My question is if there are any other methods of solving this? Maybe
a socket option of some sort...

If I'm correct (please correct me if I'm not), on Linux you can use
'sysctl -w net.ipv4.tcp_fin_timeout=X' to set the time between closing
the socket and releasing it to be reused. You can also check the
SO_REUSEADDR argument to the setsockopt function. Read 'man 7 socket'
for more info.
I've read about SO_REUSEADDR. As far as I understand, this is what
SO_REUSEADDR is for:

1. Allow a listening socket to bind itself to its well-known port even
if previously established connections use it as their local port.
Setting this option should be done between calls to socket and bind, and
hence is only usable for listening sockets, not client sockets like mine.

2. Allow multiple servers on the same host with different ip-adresses to
listen to the same port.

I've tried setting this option, but could not see any notable changes...
>
Sybren
Sep 3 '06 #2
I've read about SO_REUSEADDR. As far as I understand, this is what
SO_REUSEADDR is for:
....
I've tried setting this option, but could not see any notable changes...
I was having a similiar problem as you, where as soon as my program
exited, it would get started up again, but could not bind to the same
address.
So i added the follow straight after I create my server object:
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

And it worked. Note that my program was running on Linux, so this might
be a windows issue.

Sep 3 '06 #3
Tor Erik wrote:
The reason is that my application does about 16 connects and data
transfers per second, to the same 16 remote hosts. After approx 200 secs
there are 4000 sockets waiting to be garbage collected by the OS.
what does "netstat" say about these sockets ?

</F>

Sep 3 '06 #4
Fredrik Lundh wrote:
Tor Erik wrote:
>The reason is that my application does about 16 connects and data
transfers per second, to the same 16 remote hosts. After approx 200
secs there are 4000 sockets waiting to be garbage collected by the OS.

what does "netstat" say about these sockets ?

</F>
They are in the TIME_WAIT state... The msn library has an article on how
to solve this:

http://msdn.microsoft.com/library/de...1fc8ba06a4.asp

Summing up one could either:

1. Increase the upper range of ephemeral ports that are dynamically
allocated to client TCP/IP socket connections:

set registry key:
KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Service s\Tcpip\Parameters\MaxUserPort
to a new DWORD value... (5000 - 65534)
The default in XP is 3976 -http://support.microsoft.com/kb/Q149532

or

2. Reduce the client TCP/IP socket connection timeout value from the
default value of 240 seconds

set registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\Tcpip\Parameters\TcpTimedWaitDelay
to a new DWORD value (30 - 300)

The TCP RFC (RFC 793) recommends a value of 2*msl(Maximum Segment
Lifetime). The general consensus about the value of msn seems to be 1-2
minutes, depending on the underlying network... (2*2 min = 2*120 sec =
240 sec)
I do not want to alter my registry, so I'm currently testing an idea
where I let the client connect and send its content, appended with my
own "magic" EOF byte-sequence. When the server receives this EOF, it
takes care to close the connection. This should eliminate the problem as
it is the peer closing the connection that enters the TIME_WAIT state...

I will report my experiences...
Sep 3 '06 #5
ke***********@gmail.com wrote:
>>I've read about SO_REUSEADDR. As far as I understand, this is what
SO_REUSEADDR is for:

....
>>I've tried setting this option, but could not see any notable changes...


I was having a similiar problem as you, where as soon as my program
exited, it would get started up again, but could not bind to the same
address.
So i added the follow straight after I create my server object:
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

And it worked. Note that my program was running on Linux, so this might
be a windows issue.
.... and note also that your program was apprently a server, while the OP
was reporting an error on a client program that presumably asks for an
ephemeral port rather than a specifically-numbered one.

Since there are roughly 64,000 ports, the real question seems to be why
his client runs out after about 4,000.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 3 '06 #6
Tor Erik wrote:
Fredrik Lundh wrote:
>Tor Erik wrote:
>>The reason is that my application does about 16 connects and data
transfers per second, to the same 16 remote hosts. After approx 200
secs there are 4000 sockets waiting to be garbage collected by the OS.

what does "netstat" say about these sockets ?

</F>

They are in the TIME_WAIT state... The msn library has an article on how
to solve this:

http://msdn.microsoft.com/library/de...1fc8ba06a4.asp
Summing up one could either:

1. Increase the upper range of ephemeral ports that are dynamically
allocated to client TCP/IP socket connections:

set registry key:
KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Service s\Tcpip\Parameters\MaxUserPort

to a new DWORD value... (5000 - 65534)
The default in XP is 3976 -http://support.microsoft.com/kb/Q149532

or

2. Reduce the client TCP/IP socket connection timeout value from the
default value of 240 seconds

set registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\Tcpip\Parameters\TcpTimedWaitDelay

to a new DWORD value (30 - 300)

The TCP RFC (RFC 793) recommends a value of 2*msl(Maximum Segment
Lifetime). The general consensus about the value of msn seems to be 1-2
minutes, depending on the underlying network... (2*2 min = 2*120 sec =
240 sec)
I do not want to alter my registry, so I'm currently testing an idea
where I let the client connect and send its content, appended with my
own "magic" EOF byte-sequence. When the server receives this EOF, it
takes care to close the connection. This should eliminate the problem as
it is the peer closing the connection that enters the TIME_WAIT state...

I will report my experiences...
Well... my idea does not work as expected. Even though the server
(remote host) calls socket.close(), it is the client that executes
TIME_WAIT. My guess is that the subtrates below socket closes the
connection at the peer calling connect regardless of where socket.close
is called.

Thoughts anyone?
Sep 3 '06 #7
Tor Erik wrote:
Tor Erik wrote:
>>Fredrik Lundh wrote:
>>>Tor Erik wrote:
The reason is that my application does about 16 connects and data
transfers per second, to the same 16 remote hosts. After approx 200
secs there are 4000 sockets waiting to be garbage collected by the OS.

what does "netstat" say about these sockets ?

</F>
They are in the TIME_WAIT state... The msn library has an article on how
to solve this:

http://msdn.microsoft.com/library/de...1fc8ba06a4.asp
Summing up one could either:

1. Increase the upper range of ephemeral ports that are dynamically
allocated to client TCP/IP socket connections:

set registry key:
KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servi ces\Tcpip\Parameters\MaxUserPort

to a new DWORD value... (5000 - 65534)
The default in XP is 3976 -http://support.microsoft.com/kb/Q149532

or

2. Reduce the client TCP/IP socket connection timeout value from the
default value of 240 seconds

set registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Serv ices\Tcpip\Parameters\TcpTimedWaitDelay

to a new DWORD value (30 - 300)

The TCP RFC (RFC 793) recommends a value of 2*msl(Maximum Segment
Lifetime). The general consensus about the value of msn seems to be 1-2
minutes, depending on the underlying network... (2*2 min = 2*120 sec =
240 sec)
I do not want to alter my registry, so I'm currently testing an idea
where I let the client connect and send its content, appended with my
own "magic" EOF byte-sequence. When the server receives this EOF, it
takes care to close the connection. This should eliminate the problem as
it is the peer closing the connection that enters the TIME_WAIT state...

I will report my experiences...


Well... my idea does not work as expected. Even though the server
(remote host) calls socket.close(), it is the client that executes
TIME_WAIT. My guess is that the subtrates below socket closes the
connection at the peer calling connect regardless of where socket.close
is called.

Thoughts anyone?
Yes, it's the transport layer that puts the socket into the TIME_WAIT state.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 3 '06 #8
Steve Holden <st***@holdenweb.comwrote:
...
>set registry key:
>>KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servi ces\Tcpip\Parameters\M
axUserPort
>
to a new DWORD value... (5000 - 65534)
The default in XP is 3976 -http://support.microsoft.com/kb/Q149532
I wonder why (performance under RAM-costrained conditions? but then why
not have this vary depending on available RAM -- complications?)
Yes, it's the transport layer that puts the socket into the TIME_WAIT state.
Yes, there's a good explanation at
<http://www.developerweb.net/forum/showthread.php?t=2941(though one
should really study Stevens' "TCP-IP Illustrated" for better
understanding in depth). Playing with SO_LINGER and/or the MSL is not
recommended unless you're operating only on a network that you entirely
control (particularly in terms of round-trip times and router behavior).

As debated at
<http://www.sunmanagers.org/pipermail...ry/007068.html
>, you may be able to have your clients go into CLOSE_WAIT (instead of
TIME_WAIT) by playing around with "who closes the socket first", and
CLOSE_WAIT might be more transient than the 2*MSL (240 seconds...)
needed for TIME_WAIT on possibly-unreliable networks. But it's far from
sure that this would gain you much.

Reflecting on the OP's use case, since all connections are forever being
made to the same 16 servers, why not tweak thinks a bit to hold those
connections open for longer periods of time, using a connection for many
send/receive "transactions" instead of opening and closing such
connections all of the time? That might well work better...
Alex
Sep 3 '06 #9
2006/9/3, Alex Martelli <al***@mac.com>:
Reflecting on the OP's use case, since all connections are forever being
made to the same 16 servers, why not tweak thinks a bit to hold those
connections open for longer periods of time, using a connection for many
send/receive "transactions" instead of opening and closing such
connections all of the time? That might well work better...
Connecting to 16 differente servers per second gives a very poor
performance, right? There's some overhead in creating TCP connections,
even on fast networks and computers. Am I right?

--
Felipe.
Sep 3 '06 #10
Felipe Almeida Lessa <fe**********@gmail.comwrote:
2006/9/3, Alex Martelli <al***@mac.com>:
Reflecting on the OP's use case, since all connections are forever being
made to the same 16 servers, why not tweak thinks a bit to hold those
connections open for longer periods of time, using a connection for many
send/receive "transactions" instead of opening and closing such
connections all of the time? That might well work better...

Connecting to 16 differente servers per second gives a very poor
performance, right? There's some overhead in creating TCP connections,
even on fast networks and computers. Am I right?
There is some overhead, yes, but 16 connections per second are few
enough that I wouldn't expect that to be material (on networks with low
latency -- it might be different if you have "long fat pipes", networks
with huge bandwidth and thus "fast" but with high latency -- several
aspects of TCP/IP don't work all that well with those).

For example, try tweaking examples 1 and 2 for chapter 19 of "Python in
a Nutshell" (you can freely download the zipfile w/all examples from
<http://examples.oreilly.com/pythonian/pythonian-examples.zip>) and do
some experiments. I ran the unmodified server on an iBook G4, and on a
Macbook Pro I ran a client that looped 100 times, connecting, sending
'ciao', receiving the response, and printing a timestamp and the
response just received -- with the two laptops, sitting next to each
other, connected via Ethernet (just 100 mbps on the iBook, thus the
gigabit ethernet on the MBP wasn't really being used well;-) through a
direct cable (using ifconfig by hand to make a tiny 192.168.1/24 LAN
there;-). The 100 iterations, all together, took a bit more than half a
second (and, of course, one could try tweaking the asyncore or Twisted
examples from the same chapter to let many connections happen at once,
rather than having them "in sequence" as I just did - and many other
such small but interesting experiments). As a further data point, I
then reran the same experiment after removing the Ethernet cable, so
that the laptops were now connecting through wi-fi (802.11g, 54mbps, the
router being an Airport Express -- everything in the same room, within a
radius of 2 meters, excellent signal reception of course;-): ran in this
way, the 100 iterations took a total of over 2 seconds.
Alex
Sep 3 '06 #11
Felipe Almeida Lessa wrote:
2006/9/3, Alex Martelli <al***@mac.com>:
Reflecting on the OP's use case, since all connections are forever being
made to the same 16 servers, why not tweak thinks a bit to hold those
connections open for longer periods of time, using a connection for many
send/receive "transactions" instead of opening and closing such
connections all of the time? That might well work better...

Connecting to 16 differente servers per second gives a very poor
performance, right? There's some overhead in creating TCP connections,
even on fast networks and computers. Am I right?
I can think of four costs you would invoke by not reusing connections:
1) extra sockets. This is what the OP experienced.
2) startup/teardown bandwidth. More packets need to be sent to start
or end a connection
3) startup latency. It takes some time to create a usable connection.
4) rampup time. TCP/IP doesn't dump everything on the network as soon
as a connection is opened. It "feels it out", giving it more, then a
little more, until it finds the limit. If you're sending multiple
files (small ones especially!) you'll likely hit this.

So yeah, bottom line, it IS faster and more efficient to reuse
connections. If you're doing a protocol for sending files you'll want
to do it.

--
Adam Olsen, aka Rhamphoryncus

Sep 5 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tig | last post by:
Hi all. I have a need to connect to an Oracle 7.3.3.5 database. I have a user who successfully connects to it with her Oracle 7.3 client. I have an Oracle 9.2 client installed on my machine....
7
by: Privacy Advocate | last post by:
//crossposted to: comp.lang.javascript, alt.comp.lang.javascript in an effort to get factual answers from JavaScript experts// Simply put; Is it possible to obtain the real (actual) IP address of...
30
by: aka | last post by:
Hi I have a DB2 v8.1 on AIX and DB2 Connect EE on Solaris wich is connected to OS/390 DB2 subsystems via APPC / SNA. I have cataloged the DB2 Connect instance as tcpip node and then the Host DB...
1
by: luciano | last post by:
Hi everyone, I want to create a application and a webservice, application connect to web service to activate, web sevice will create a certificate to authenticate this client, for each...
7
by: rob | last post by:
I want to create an application that can be used both for a server-client model as well as a standalong application. The idea is to write a general server-client application. The standalong then...
3
by: D. Yates | last post by:
Hi, I'm about to embark on a project that will both send and receive information to/from our client computers in the field. The area that I still need to finalize is the method of...
3
by: Laurence | last post by:
Hi there, Does anyone know what's difference among "connect reset", "disconnect", and "terminate"? Thanks in advance,
3
by: RFD | last post by:
I've been slaving at this problem for over a week, and would appreciate some help from you kind folks. Basic Problem: I have made a server program and a client program. When I try to use the...
2
by: jeffhan | last post by:
we have os/400 db2 database at the backend. i installed db2 v9 connect server on one windows server which is locating the same network with database server. now how to i configure the connect...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.