Use socket opt to prevent a lingering socket after program close
This commit is contained in:
@@ -25,6 +25,12 @@ ListeningSocket::ListeningSocket(int const port)
|
||||
throw std::runtime_error("socket creation error");
|
||||
}
|
||||
|
||||
int enable = 1;
|
||||
if (setsockopt(socketFileDescriptor, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
|
||||
{
|
||||
throw std::runtime_error("setsockopt SO_REUSEADDR failed");
|
||||
}
|
||||
|
||||
socketAddress.sin_family = AF_INET;
|
||||
socketAddress.sin_addr.s_addr = INADDR_ANY;
|
||||
socketAddress.sin_port = htons(port);
|
||||
|
||||
Reference in New Issue
Block a user