Socket: Use SO_REUSEADDR, disable SO_LINGER

Use these to avoid "socket in use" errors when restarting a DAP that uses the same port.
This commit is contained in:
Ben Clayton 2020-01-28 14:04:23 +00:00
parent 9a9d46f6b6
commit de7dffaf66

View File

@ -74,6 +74,21 @@ class dap::Socket::Shared : public dap::ReaderWriter {
if (info) { if (info) {
auto socket = auto socket =
::socket(info->ai_family, info->ai_socktype, info->ai_protocol); ::socket(info->ai_family, info->ai_socktype, info->ai_protocol);
#if !defined(_WIN32)
// Prevent sockets lingering after process termination, causing
// reconnection issues on the same port.
int enable = 1;
setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
struct {
int l_onoff; /* linger active */
int l_linger; /* how many seconds to linger for */
} linger = {false, 0};
setsockopt(socket, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
#endif // !defined(_WIN32)
return std::make_shared<Shared>(info, socket); return std::make_shared<Shared>(info, socket);
} }