SO_REUSEADDR的用例是什么? [英] What are the use cases of SO_REUSEADDR?

查看:87
本文介绍了SO_REUSEADDR的用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 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()之前加一个前缀,则可以将多个进程绑定到同一 SOCK_DGRAM UDP端口:

More than one process may bind to the same SOCK_DGRAM UDP port if the bind() is preceded by:

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的用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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