Skip to content

Commit 80202c9

Browse files
authored
Merge pull request yhirose#292 from Bendr0id/fix_socket_create_on_older_windows_systems
Adds workaround for socket creation on older Windows variants
2 parents 39c7bba + 094a6a6 commit 80202c9

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

httplib.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,22 @@ socket_t create_socket(const char *host, int port, Fn fn,
13021302
#ifdef _WIN32
13031303
auto sock = WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol,
13041304
nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT);
1305+
/**
1306+
* Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1 and above
1307+
* the socket creation fails on older Windows Systems.
1308+
*
1309+
* Let's try to create a socket the old way in this case.
1310+
*
1311+
* Reference:
1312+
* https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa
1313+
*
1314+
* WSA_FLAG_NO_HANDLE_INHERIT:
1315+
* This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with SP1, and later
1316+
*
1317+
*/
1318+
if (sock == INVALID_SOCKET) {
1319+
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1320+
}
13051321
#else
13061322
auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
13071323
#endif

0 commit comments

Comments
 (0)