作者:迷失千夏——拾露奖云
性参考: Currently,linuxdoesnotallowreuseofsamelocalportforbothtcpserverandtcpclient .然后小测试程序成功了,和我的库测试程序
其他资料:
这是最大的骚动。 对不起。 linux没有端口重用吗? 在SO_REUSEPORT报纸上找不到
搞定了。/usr/include/ASM/socket.h :/* toadd : # defineso _ reuse port 15 * /
. b1f4190ff47707.html
freebsd和linux下绑定系统调用总结
(仅考虑AF_INET的情况(所谓相同的端口,意味着ip地址和端口号相同) ) ) ) ) ) ) ) ) ) )。
1.freebsd支持SO_REUSEPORT和SO_REUSEADDR选项,linux仅支持SO_REUSEADDR选项。
2 .在2.freebsd中,可以使用SO_REUSEPORT选项将两个tcp的套接字绑定到同一端口; 同样,可以使用SO_REUSEPORT选项将两个udp的套接字绑定到同一端口。
3 .在3.linux上,两个tcp的套接字不能绑定到同一端口; 使用SO_REUSEADDR选项,可以将两个udp的套接字绑定到同一端口。
4 .在4.freebsd中,两个tcp的套接字绑定到同一端口,并且只有第一个套接字检索数据。
5 .在5.freebsd中,两个udp的套接字绑定到同一端口。 如果数据包的目标地址是单播地址,则只有第一个套接字获取数据;如果数据包的目标地址是多播地址,则两个套接字同时获取相同的数据。
6 .在6.linux上,两个udp的套接字绑定到同一端口。 如果包的目标地址是单播地址,则只有最后一个套接字获取数据。 如果包的目标地址是多播地址,则两个套接字同时获取相同的数据。
Unix网络API
SO_REUSEADDR和SO_REUSEPORT
SO_REUSEADDR提供以下四个功能:
使用SO_REUSEADDR,即使以前建立的连接用作本地端口,也可以启动监听服务器并捆绑熟知的端口。 这通常在监听服务器重新启动时发生。 如果不设置此选项,绑定时将发生错误。
在SO_REUSEADDR中,如果每个实例捆绑了不同的本地IP地址,则可以在同一端口上启动同一服务器的多个实例。 对于TCP,不能启动捆绑了相同IP地址和相同端口号的多个服务器。
使用SO_REUSEADDR,一个进程可以将同一端口与多个套接字捆绑在一起,方法是为每个捆绑包指定不同的本地IP地址。 这一般不用于TCP服务器。
SO_REUSEADDR允许完整的重复捆绑。 如果一个IP地址和端口绑定到一个套接字,则该IP地址和端口也绑定到另一个套接字。 通常,此特性仅用于支持多播的系统,而仅用于UDP套接字接口(TCP不支持多播)。
SO_REUSEPORT选项具有以下含义:
此选项允许完整的重复捆绑包,但仅指定给尝试捆绑相同IP地址和端口的套接字。
如果捆绑的IP地址是多播地址,则SO_REUSEADDR和SO_REUSEPORT是等效的。
使用这两个接口选项的建议:
在所有TCP服务器上,在调用bind之前设置SO_REUSEADDR套接字接口选项。
如果要创建可在同一台主机上同时运行多次的多播APP应用程序,请设置SO_REUSEADDR选项,并将本组的多播地址作为本地IP地址捆绑在一起。
watisthedifferencebetweenso _ reuseaddrandso _ reuse port?
from:UNIX套接字常见问题解答
so _ reuseaddrallowsyourservertobindtoanaddresswhichisina
time _ wait state.itdoesnotallowmorethanoneservertobindtothe
same address.itwasmentionedthatuseofthisflagcancreatea
securityriskbecauseanotherservercanbindtoathesameport,by
bindingtoaspecificaddressasopposedtoinaddr _ any.the
so _ reuseportflagallowsmultipleprocessestobindtothesameaddress
providedallofthemusetheso _ reuseportoption。
f
rom Richard Stevens (rstevens@noao.edu):This is a newer flag that appeared in the 4.4BSD multicasting code
(although that code was from elsewhere, so I am not sure just who
invented the new SO_REUSEPORT flag).
What this flag lets you do is rebind a port that is already in use, but
only if all users of the port specify the flag. I believe the intent is
for multicasting apps, since if you're running the same app on a host,
all need to bind the same port. But the flag may have other uses. For
example the following is from a post in February:
From Stu Friedberg (stuartf@sequent.com):
SO_REUSEPORT is also useful for eliminating the try-10-times-to-bind
hack in ftpd's data connection setup routine. Without SO_REUSEPORT,
only one ftpd thread can bind to TCP (lhost, lport, INADDR_ANY, 0) in
preparation for connecting back to the client. Under conditions of
heavy load, there are more threads colliding here than the try-10-times
hack can accomodate. With SO_REUSEPORT, things work nicely and the hack
becomes unnecessary.
I have also heard that DEC OSF supports the flag. Also note that under
4.4BSD, if you are binding a multicast address, then SO_REUSEADDR is
condisered the same as SO_REUSEPORT (p. 731 of "TCP/IP Illustrated,
Volume 2"). I think under Solaris you just replace SO_REUSEPORT with
SO_REUSEADDR.
From a later Stevens posting, with minor editing:
Basically SO_REUSEPORT is a BSD'ism that arose when multicasting was
added, even thought it was not used in the original Steve Deering code.
I believe some BSD-derived systems may also include it (OSF, now
Digital Unix, perhaps?). SO_REUSEPORT lets you bind the same address
*and* port, but only if all the binders have specified it. But when
binding a multicast address (its main use), SO_REUSEADDR is considered
identical to SO_REUSEPORT (p. 731, "TCP/IP Illustrated, Volume 2"). So
for portability of multicasting applications I always use SO_REUSEADDR.
阅读(860) | 评论(0) | 转发(0) |