Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -10902,13 +10902,24 @@ inline bool setup_client_tls_session(
*/

inline void default_socket_options(socket_t sock) {
#ifdef _WIN32
// On Windows, SO_REUSEADDR has a different, dangerous meaning: it allows a
// socket to bind to a port that another socket is already bound to (i.e.
// the bind silently steals the port) instead of returning WSAEADDRINUSE.
// Use SO_EXCLUSIVEADDRUSE to get the POSIX-like exclusive binding; a bind
// to an occupied port then fails with WSAEADDRINUSE. (SO_EXCLUSIVEADDRUSE
// only affects bind, so this is harmless for client sockets which connect
// without binding.)
set_socket_opt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, 1);
#else
set_socket_opt(sock, SOL_SOCKET,
#ifdef SO_REUSEPORT
SO_REUSEPORT,
#else
SO_REUSEADDR,
#endif
1);
#endif
}

inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) {
Expand Down