客户端TCP套接字接收“已在使用的地址”连接时 [英] Client-side TCP socket receiving "Address already in use" upon connect

查看:93
本文介绍了客户端TCP套接字接收“已在使用的地址”连接时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




原因是我的应用程序每秒向同一个16个远程主机执行大约16次连接和数据
传输。大约200秒后,
有4000个套接字等待操作系统进行垃圾回收。在

这一点似乎连接循环并开始使用它使用4000个连接前的相同的本地

地址,从而产生地址已经

in use例外。


一个可能的解决方案是保持与每个

远程主机的连接打开,但这需要解析收到的数据

到应用程序发送的数据项(顺便说一下是文件)。


我的问题是,是否还有其他方法可以解决这个问题?也许是某种类型的

套接字选项...


问候

解决方案

< blockquote> Sybren Stuvel写道:


Tor Erik用以下方式启发我们:


>原因是我的应用程序每秒执行大约16次连接和数据传输,到相同的16个远程主机。大约200
秒后,有44个套接字等待由
操作系统进行垃圾回收。



我们在谈论哪种操作系统?



Windows XP


>


>此时似乎连接循环并开始使用与之前4000连接相同的本地地址,从而产生了一个
地址已在使用中例外。



这会发生多少秒?



约200秒


>


>我的问题是,是否还有其他方法可以解决这个问题?也许
某种类型的套接字选项...



如果我是正确的(请纠正我,如果我不是),在Linux上你可以使用

''sysctl -w net.ipv4.tcp_fin_timeout = X''来设置关闭套接字和释放它以重复使用之间的时间。您还可以检查setsockopt函数的

SO_REUSEADDR参数。阅读''man 7 socket''

了解更多信息。



我读过SO_REUSEADDR。据我所知,这是什么

SO_REUSEADDR用于:


1.允许侦听套接字将自身绑定到其知名端口甚至<如果以前建立的连接使用它作为本地端口,则需要


设置此选项应该在调用socket和bind之间完成,并且

因此是只能用于收听套接字,而不是像我这样的客户端套接字。


2.允许同一台主机上的多台服务器使用不同的ip-adresses

听取同样的端口。


我试过设置这个选项,但看不到任何显着的变化......


>

Sybren


我读过SO_REUSEADDR。据我所知,这是


SO_REUSEADDR适用于:



....


我尝试过设置此选项,但看不到任何显着的变化...



我遇到了类似的问题,只要我的程序退出,它就会再次启动,但无法绑定到相同的

地址。 br />
所以我在创建服务器对象后添加了直接跟随:

server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)


它有效。请注意,我的程序在Linux上运行,所以这可能是一个Windows问题。


Tor Erik写道:


原因是我的应用程序每秒向相同的16个远程主机执行大约16次连接和数据
传输。大约200秒后,
有4000个套接字等待操作系统进行垃圾回收。



什么是netstat说这些插座?


< / F>


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

解决方案

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


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.


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>


这篇关于客户端TCP套接字接收“已在使用的地址”连接时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆