diff --git a/src/cpp/transport/tcp/TCPServerLinux.cpp b/src/cpp/transport/tcp/TCPServerLinux.cpp index 446fbf205..a6e3e86cf 100644 --- a/src/cpp/transport/tcp/TCPServerLinux.cpp +++ b/src/cpp/transport/tcp/TCPServerLinux.cpp @@ -78,6 +78,17 @@ bool TCPv4Agent::init() if (-1 != listener_poll_.fd) { + /* Set reuse port. */ + int reuse = 1; + if (-1 == setsockopt(listener_poll_.fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) + { + UXR_AGENT_LOG_ERROR( + UXR_DECORATE_RED("socket opt error"), + "Port: {}", + listener_poll_.fd); + return false; + } + /* IP and Port setup. */ struct sockaddr_in address; address.sin_family = AF_INET; diff --git a/src/cpp/transport/tcp/TCPServerWindows.cpp b/src/cpp/transport/tcp/TCPServerWindows.cpp index 452cbc378..eac0ed7a1 100644 --- a/src/cpp/transport/tcp/TCPServerWindows.cpp +++ b/src/cpp/transport/tcp/TCPServerWindows.cpp @@ -68,6 +68,17 @@ bool TCPv4Agent::init() if (INVALID_SOCKET != listener_poll_.fd) { + /* Set reuse port. */ + int reuse = 1; + if (-1 == setsockopt(listener_poll_.fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse))) + { + UXR_AGENT_LOG_ERROR( + UXR_DECORATE_RED("socket opt error"), + "Port: {}", + listener_poll_.fd); + return false; + } + /* IP and Port setup. */ struct sockaddr_in address; address.sin_family = AF_INET;