InetPtonW 总是 returns 1.0.0.0
InetPtonW always returns 1.0.0.0
我刚开始学习C,现在正在学习如何使用winsock2-Header。要将 ip 地址从字符串表示形式转换为二进制形式,我使用函数 InetPtonW
。我的问题是这个函数总是 returns IP 地址 1.0.0.0.
我在 Windows 10 上使用 Visual Studio。我阅读了 Microsoft 的文档(请参阅 Whosebug 上的 here) and I also read this 问题。我使用其他数据类型尝试了不同的方法,但我可以没有得到正确的结果。
下面是我的问题的相关代码。
int main() {
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
PCWSTR pStringIp = (L"172.217.168.14"); //IP for own webserver
PVOID pAddrBuf;
char* message;
char* recvbuf; // Buffer for the reply
int recvbuflen = DEFAULT_BUFLEN; //Size of the reply from the server
/* Socket creation etc. would be here */
pAddrBuf = malloc(INET6_ADDRSTRLEN);
if (pAddrBuf == NULL) {
printMemoryErrorFailMessage(errno, pAddrBuf);
return EXIT_FAILURE;
}
server.sin_addr.s_addr = InetPtonW(AF_INET, pStringIp, pAddrBuf);
free(pAddrBuf);
}
我希望示例中的 IP 地址将转换为 172.217.168.14 而不是 1.0.0.0
如果您需要更多信息或更多代码,请询问。感谢您的帮助。
请
Necessary_Function
来自the InetPtonW
documentation:
对于参数pAddrBuf
A pointer to a buffer in which to store the numeric binary representation of the IP address. The IP address is returned in network byte order.
为返回值
If no error occurs, the InetPton
function returns a value of 1
and the buffer pointed to by the pAddrBuf
parameter contains the binary numeric IP address in network byte order.
总结一下:函数 returns 一个布尔值 1
或 0
取决于它是成功还是失败;如果成功,它会将地址写入 pAddrBuffer
(第三个参数)指向的内存中。
你得到地址1.0.0.0
的原因是因为你使用返回的布尔结果作为地址,而丢弃了写入pAddrBuf
指向的内存的实际地址。
使用该函数的“正确”方式应该是这样的:
if (InetPtonW(AF_INET, pStringIp, &server.sin_addr.s_addr) == 1)
{
// Success, use the address some way
}
我刚开始学习C,现在正在学习如何使用winsock2-Header。要将 ip 地址从字符串表示形式转换为二进制形式,我使用函数 InetPtonW
。我的问题是这个函数总是 returns IP 地址 1.0.0.0.
我在 Windows 10 上使用 Visual Studio。我阅读了 Microsoft 的文档(请参阅 Whosebug 上的 here) and I also read this 问题。我使用其他数据类型尝试了不同的方法,但我可以没有得到正确的结果。
下面是我的问题的相关代码。
int main() {
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
PCWSTR pStringIp = (L"172.217.168.14"); //IP for own webserver
PVOID pAddrBuf;
char* message;
char* recvbuf; // Buffer for the reply
int recvbuflen = DEFAULT_BUFLEN; //Size of the reply from the server
/* Socket creation etc. would be here */
pAddrBuf = malloc(INET6_ADDRSTRLEN);
if (pAddrBuf == NULL) {
printMemoryErrorFailMessage(errno, pAddrBuf);
return EXIT_FAILURE;
}
server.sin_addr.s_addr = InetPtonW(AF_INET, pStringIp, pAddrBuf);
free(pAddrBuf);
}
我希望示例中的 IP 地址将转换为 172.217.168.14 而不是 1.0.0.0
如果您需要更多信息或更多代码,请询问。感谢您的帮助。
请 Necessary_Function
来自the InetPtonW
documentation:
对于参数
pAddrBuf
A pointer to a buffer in which to store the numeric binary representation of the IP address. The IP address is returned in network byte order.
为返回值
If no error occurs, the
InetPton
function returns a value of1
and the buffer pointed to by thepAddrBuf
parameter contains the binary numeric IP address in network byte order.
总结一下:函数 returns 一个布尔值 1
或 0
取决于它是成功还是失败;如果成功,它会将地址写入 pAddrBuffer
(第三个参数)指向的内存中。
你得到地址1.0.0.0
的原因是因为你使用返回的布尔结果作为地址,而丢弃了写入pAddrBuf
指向的内存的实际地址。
使用该函数的“正确”方式应该是这样的:
if (InetPtonW(AF_INET, pStringIp, &server.sin_addr.s_addr) == 1)
{
// Success, use the address some way
}