问题描述
我已经使用 SO_REUSEADDR
来让我的服务器被终止重新启动而没有抱怨套接字已经在使用中.我想知道 SO_REUSEADDR
是否还有其他用途?是否有人将套接字选项用于上述目的以外的用途?
I have used SO_REUSEADDR
to have my server which got terminated to restart with out complaining that the socket is already is in use. I was wondering if there are other uses of SO_REUSEADDR
? Have anyone used the socket option for other than the said purpose?
推荐答案
对于 TCP,主要目的是在同一地址重新启动已关闭/终止的进程.
For TCP, the primary purpose is to restart a closed/killed process on the same address.
需要该标志是因为端口进入 TIME_WAIT
状态以确保传输所有数据.
The flag is needed because the port goes into a TIME_WAIT
state to ensure all data is transferred.
如果两个套接字绑定到相同的接口和端口,并且它们是同一个多播组的成员,则数据将传送到两个套接字.
If two sockets are bound to the same interface and port, and they are members of the same multicast group, data will be delivered to both sockets.
我想另一种用途是尝试拦截数据的安全攻击.
I guess an alternative use would be a security attack to try to intercept data.
(来源)
对于UDP,SO_REUSEADDR
用于多播.
如果 bind()
前面有:
int one = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
在这种情况下,每个传入的多播或广播 UDP 数据报注定共享端口被传递给绑定到该端口的所有套接字.
In this case, every incoming multicast or broadcast UDP datagram destined to the shared port is delivered to all sockets bound to the port.
(来源)
这篇关于SO_REUSEADDR 的用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!