Skip to content

Commit

Permalink
net/RBindSocket: enable SO_REUSEADDR
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jan 5, 2024
1 parent d2c4fd9 commit 540fdc8
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/net/RBindSocket.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ ResolveBindSocket(const char *host_and_port, int default_port,
if (!s.CreateNonBlock(ai.GetFamily(), ai.GetType(), ai.GetProtocol()))
throw MakeSocketError("Failed to create socket");

if (ai.IsTCP())
/* always set SO_REUSEADDR for TCP sockets to allow
quick restarts */
s.SetReuseAddress();

if (!s.Bind(ai))
throw MakeSocketError("Failed to bind");

Expand All @@ -37,6 +42,11 @@ ParseBindSocket(const char *host_and_port, int default_port, int socktype)
if (!s.CreateNonBlock(address.GetFamily(), socktype, 0))
throw MakeSocketError("Failed to create socket");

if (address.IsInet() && socktype == SOCK_STREAM)
/* always set SO_REUSEADDR for TCP sockets to allow
quick restarts */
s.SetReuseAddress();

if (!s.Bind(address))
throw MakeSocketError("Failed to bind");

Expand Down

0 comments on commit 540fdc8

Please sign in to comment.