Skip to content

Commit 65abf63

Browse files
Copilotrdementi
andcommitted
Fix IPv4 socket binding on non-Windows systems
The IPv4 binding code was incorrectly wrapped in #ifdef _WIN32, causing the server to fall through to IPv6 binding even when IPv4 was explicitly requested with the -4 flag on Linux systems. This fix removes the Windows- specific conditional around the IPv4 binding logic while keeping platform- specific socket close calls. Fixes the issue where pcm-sensor-server with -4 flag would fail to accept connections from curl and wget on Linux. Co-authored-by: rdementi <[email protected]>
1 parent 03f48cb commit 65abf63

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/pcm-sensor-server.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,6 @@ class Server {
14671467
int retval = 0;
14681468

14691469
if (useIPv4) {
1470-
#ifdef _WIN32
14711470
// Use IPv4
14721471
struct sockaddr_in serv4;
14731472
memset(&serv4, 0, sizeof(serv4));
@@ -1479,13 +1478,16 @@ class Server {
14791478
if ( 1 != ::inet_pton( AF_INET, listenIP_.c_str(), &(serv4.sin_addr) ) )
14801479
{
14811480
DBG( 3, "close clientsocketFD" );
1481+
#ifdef _WIN32
1482+
closesocket(sockfd);
1483+
#else
14821484
::close(sockfd);
1485+
#endif
14831486
throw std::runtime_error(std::string("Server Constructor: Cannot convert IP string ") + listenIP_ + " to IPv4 address");
14841487
}
14851488
}
14861489
socklen_t len = sizeof( struct sockaddr_in );
14871490
retval = ::bind( sockfd, reinterpret_cast<struct sockaddr*>(&serv4), len );
1488-
#endif
14891491
} else {
14901492
// Use IPv6
14911493
struct sockaddr_in6 serv;

0 commit comments

Comments
 (0)