mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
Use InetNtop on Win32 rather than WSAAddressToString. (#170)
Similarly, use InetPton rather than WSAStringToAddress. The WSAAddressToString function is intended to provide a user-readable string and thus includes the port number. This breaks some use cases on Windows which expect to get just the IP address. Note: The InetPton and InetNtop functions are available only in Vista or above.
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include <cstring>
|
||||
#ifdef _WIN32
|
||||
#include <WinSock2.h>
|
||||
#include <Ws2tcpip.h>
|
||||
#pragma comment(lib, "Ws2_32.lib")
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
@@ -85,11 +86,14 @@ int TCPAcceptor::start() {
|
||||
#ifdef _WIN32
|
||||
llvm::SmallString<128> addr_copy(m_address);
|
||||
addr_copy.push_back('\0');
|
||||
int size = sizeof(address);
|
||||
WSAStringToAddress(addr_copy.data(), PF_INET, nullptr, (struct sockaddr*)&address, &size);
|
||||
int res = InetPton(PF_INET, addr_copy.data(), &(address.sin_addr));
|
||||
#else
|
||||
inet_pton(PF_INET, m_address.c_str(), &(address.sin_addr));
|
||||
int res = inet_pton(PF_INET, m_address.c_str(), &(address.sin_addr));
|
||||
#endif
|
||||
if (res != 1) {
|
||||
WPI_ERROR(m_logger, "could not resolve " << m_address << " address");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user