Skip to content

Commit

Permalink
[RPC] Make the port reusable after the socket is closed. (#2418)
Browse files Browse the repository at this point in the history
* reuse.

* fix compile.

Co-authored-by: Ubuntu <ubuntu@ip-172-31-2-202.us-west-1.compute.internal>
  • Loading branch information
zheng-da and Ubuntu committed Dec 15, 2020
1 parent 9d7bf4e commit 8ff4798
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/rpc/network/tcp_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ TCPSocket::TCPSocket() {
if (socket_ < 0) {
LOG(FATAL) << "Can't create new socket. Errno=" << errno;
}
#ifndef _WIN32
// This is to make sure the same port can be reused right after the socket is closed.
int enable = 1;
if (setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
LOG(WARNING) << "cannot make the socket reusable. Errno=" << errno;
}
#endif // _WIN32
}

TCPSocket::~TCPSocket() {
Expand Down

0 comments on commit 8ff4798

Please sign in to comment.