-
Notifications
You must be signed in to change notification settings - Fork 596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setReuseable sets whether the socket fd is written in reverse. #244
Comments
Let's first look at how the function is implemented, and the related functional description: int SockUtil::setReuseable(int fd, bool on = true, bool reuse_port = true) {
int opt = on ? 1 : 0;
int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, static_cast<socklen_t>(sizeof(opt)));
if (ret == -1) {
TraceL << "setsockopt SO_REUSEADDR failed";
return ret;
}
#if defined(SO_REUSEPORT)
if (reuse_port) {
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &opt, static_cast<socklen_t>(sizeof(opt)));
if (ret == -1) {
TraceL << "setsockopt SO_REUSEPORT failed";
}
}
#endif
return ret;
}
int SockUtil::setReuseable(int fd, bool on = true, bool reuse_port = true) {
int opt = on ? 1 : 0;
int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, static_cast<socklen_t>(sizeof(opt)));
if (ret == -1) {
TraceL << "setsockopt SO_REUSEADDR failed";
return ret;
}
#if defined(SO_REUSEPORT)
if (reuse_port) {
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &opt, static_cast<socklen_t>(sizeof(opt)));
if (ret == -1) {
TraceL << "setsockopt SO_REUSEPORT failed";
}
}
#endif
return ret;
}
int SockUtil::setReuseable(int fd, bool on = true, bool reuse_port = true) {
int opt = on ? 1 : 0;
int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, static_cast<socklen_t>(sizeof(opt)));
if (ret == -1) {
TraceL << "setsockopt SO_REUSEADDR failed";
return ret;
}
#if defined(SO_REUSEPORT)
if (reuse_port) {
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &opt, static_cast<socklen_t>(sizeof(opt)));
if (ret == -1) {
TraceL << "setsockopt SO_REUSEPORT failed";
}
}
#endif
return ret;
}
int SockUtil::setReuseable(int fd, bool on = true, bool reuse_port = true) {
int opt = on ? 1 : 0;
int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, static_cast<socklen_t>(sizeof(opt)));
if (ret == -1) {
TraceL << "setsockopt SO_REUSEADDR failed";
return ret;
}
#if defined(SO_REUSEPORT)
if (reuse_port) {
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &opt, static_cast<socklen_t>(sizeof(opt)));
if (ret == -1) {
TraceL << "setsockopt SO_REUSEPORT failed";
}
}
#endif
return ret;
}
|
This change was introduced in 0e040d2 to fix the issue where listening on a port twice would not throw an error.
|
SO_REUSEPORT should be turned off by default and should only be turned on in rare cases.
|
Looking at the code, after the main server listens, it calls fromSock_l->attachEvent to listen to the fd.
看代码里面,主Server listren后,调用fromSock_l->attachEvent监听fd。
|
SO_REUSEPORT is set in
connect
.However, SO_REUSEPORT is unset in
listen
.I'm a little confused. Clients don't need to use SO_REUSEPORT, right? Only the server needs it. Thanks for the explanation.
在listen中,却取消了SO_REUSEPORT设置
有点不理解,客户端没必要使用SO_REUSEPORT吧,服务端才需要,谢谢解惑。
TRANS_BY_GITHUB_AI_ASSISTANT
The text was updated successfully, but these errors were encountered: