diff --git a/ixwebsocket/IXNetSystem.cpp b/ixwebsocket/IXNetSystem.cpp index c4b71aeb..38008b54 100644 --- a/ixwebsocket/IXNetSystem.cpp +++ b/ixwebsocket/IXNetSystem.cpp @@ -278,4 +278,17 @@ namespace ix #endif } + // Convert network bytes to host bytes. Copied from the ASIO library + unsigned short network_to_host_short(unsigned short value) + { + #if defined(_WIN32) + unsigned char* value_p = reinterpret_cast(&value); + unsigned short result = (static_cast(value_p[0]) << 8) + | static_cast(value_p[1]); + return result; + #else // defined(_WIN32) + return ntohs(value); + #endif // defined(_WIN32) + } + } // namespace ix diff --git a/ixwebsocket/IXNetSystem.h b/ixwebsocket/IXNetSystem.h index ef57c6bc..f919d8db 100644 --- a/ixwebsocket/IXNetSystem.h +++ b/ixwebsocket/IXNetSystem.h @@ -81,4 +81,6 @@ namespace ix const char* inet_ntop(int af, const void* src, char* dst, socklen_t size); int inet_pton(int af, const char* src, void* dst); + + unsigned short network_to_host_short(unsigned short value); } // namespace ix diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index 91c5eb96..a242b67d 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -351,7 +351,7 @@ namespace ix continue; } - remotePort = client.sin_port; + remotePort = ix::network_to_host_short(client.sin_port); remoteIp = remoteIp4; } else // AF_INET6 @@ -371,7 +371,7 @@ namespace ix continue; } - remotePort = client.sin_port; + remotePort = ix::network_to_host_short(client.sin_port); remoteIp = remoteIp6; }