SO_REUSEADDR 对服务器有意义吗?
我正在使用两个使用 UDP 套接字的二进制文件。进程 A 通过 select()
在 UDP 套接字 (IP_1:PORT_1) 上等待消息,进程 B 最终通过 UDP 套接字发送消息。
由于某些超出范围的限制,进程 B 需要通过 (IP_1:PORT_1) 上的套接字发送。由于这两个进程的 IP:PORT 对相同,因此无法使用 bind()
。我尝试了 SO_REUSEADDR
,但是我想知道将 IP:PORT 与 SO_REUSEADDR
一起用于发送和接收是否有意义,或者这个选项只是为了监听套接字而设计的?
进程 A
int nOptVal = 1;
setsockopt(UDPSocket, SOL_SOCKET, SO_REUSEADDR, &nOptVal, sizeof(nOptVal));
bind(UDPSocket, (struct sockaddr *)&addrLocal, sizeof(addrLocal));
select(fdMax+1, UDPSocket, NULL, NULL, NULL);
进程 B
int nOptVal = 1;
setsockopt(UDPSocket, SOL_SOCKET, SO_REUSEADDR, &nOptVal, sizeof(nOptVal));
bind(UDPSocket, (struct sockaddr *)&addrLocal, sizeof(addrLocal));
sendto(UDPSocket, buff, len, 0, (struct sockaddr *)&addrDest, sizeof(struct sockaddr));
I am working with two binaries which use UDP sockets. Process A waits for messages on a UDP socket (IP_1:PORT_1) by select()
, and process B eventually sends through an UDP socket.
By some constraints out of scope, process B needs to send by a socket on (IP_1:PORT_1). Since this is the same IP:PORT pair for both processes, it is not possible to use bind()
. I tried SO_REUSEADDR
, but I am wondering if reusing the IP:PORT with SO_REUSEADDR
for sending and receiving makes sense, or was this option conceived just for listening sockets?
process A
int nOptVal = 1;
setsockopt(UDPSocket, SOL_SOCKET, SO_REUSEADDR, &nOptVal, sizeof(nOptVal));
bind(UDPSocket, (struct sockaddr *)&addrLocal, sizeof(addrLocal));
select(fdMax+1, UDPSocket, NULL, NULL, NULL);
process B
int nOptVal = 1;
setsockopt(UDPSocket, SOL_SOCKET, SO_REUSEADDR, &nOptVal, sizeof(nOptVal));
bind(UDPSocket, (struct sockaddr *)&addrLocal, sizeof(addrLocal));
sendto(UDPSocket, buff, len, 0, (struct sockaddr *)&addrDest, sizeof(struct sockaddr));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论