C++ InetPton()将任何IP转换为1.0.0.0
我试图通过向某个IP发送ping,但是C++ InetPton()将任何IP转换为1.0.0.0,c++,ip-address,ping,data-conversion,icmp,C++,Ip Address,Ping,Data Conversion,Icmp,我试图通过向某个IP发送ping,但是InetPton()函数(假定该函数将字符串IP转换为二进制形式)总是返回相同的IP:“1.0.0.0” 我的代码如下所示: short ip[4] = { 192, 168, 1, 2 }; bool checkIP() { HANDLE hIcmpFile; unsigned long ipaddr = INADDR_NONE; DWORD dwRetVal = 0; char SendData[32] = "Data
InetPton()
函数(假定该函数将字符串IP转换为二进制形式)总是返回相同的IP:“1.0.0.0”
我的代码如下所示:
short ip[4] = { 192, 168, 1, 2 };
bool checkIP() {
HANDLE hIcmpFile;
unsigned long ipaddr = INADDR_NONE;
DWORD dwRetVal = 0;
char SendData[32] = "Data Buffer";
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
std::wostringstream strIP;
strIP << ip[0] << "." << ip[1] << "." << ip[2] << "." << ip[3];
in_addr ipAddress;
ipaddr = InetPton(AF_INET, strIP.str().c_str(), &ipAddress);
if (ipaddr != 1)
{
SendMessage((HWND)StatusBar, (UINT)SB_SETTEXT, (WPARAM)(INT)1 | 0, (LPARAM)(LPSTR)TEXT("Invalid IP format!"));
return false;
}
hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE)
{
SendMessage((HWND)StatusBar, (UINT)SB_SETTEXT, (WPARAM)(INT)1 | 0, (LPARAM)(LPSTR)TEXT("Unable to open ICMP handle!"));
return false;
}
ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
ReplyBuffer = (VOID*)malloc(ReplySize);
if (ReplyBuffer == NULL)
{
SendMessage((HWND)StatusBar, (UINT)SB_SETTEXT, (WPARAM)(INT)1 | 0, (LPARAM)(LPSTR)TEXT("Unable to allocate memory!"));
return false;
}
dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 2000);
if (dwRetVal != 0)
{
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
struct in_addr ReplyAddr;
ReplyAddr.S_un.S_addr = pEchoReply->Address;
IcmpCloseHandle(hIcmpFile);
return true;
}
else {
IcmpCloseHandle(hIcmpFile);
return false;
}
return true;
}
shortip[4]={192,168,1,2};
bool checkIP(){
处理HICMP文件;
无符号长ipaddr=INADDR\u无;
DWORD dwRetVal=0;
char SendData[32]=“数据缓冲区”;
LPVOID ReplyBuffer=NULL;
DWORD ReplySize=0;
std::wostringstream条带;
剥离您的代码在这里似乎有点不对劲
in_addr* ipAddress;
ipaddr = InetPton(AF_INET, strIP.str().c_str(), &ipAddress);
ipAddress
将是一个未初始化的指针,指向\u addr
中的,您将指针传递到InetPton。InetPton真正想要的是指向它可以填充的实际结构/缓冲区的指针
in_addr ipAddress;
ipaddr = InetPton(AF_INET, strIP.str().c_str(), &ipAddress);
你的代码似乎有点不对劲
in_addr* ipAddress;
ipaddr = InetPton(AF_INET, strIP.str().c_str(), &ipAddress);
ipAddress
将是一个未初始化的指针,指向\u addr
中的,您将指针传递到InetPton。InetPton真正想要的是指向它可以填充的实际结构/缓冲区的指针
in_addr ipAddress;
ipaddr = InetPton(AF_INET, strIP.str().c_str(), &ipAddress);
应该是:
IcmpSendEcho(hIcmpFile, ipAddress, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 2000);
您的ipaddr
是1,可能这是1.0.0.0
地址的来源
应该是:
IcmpSendEcho(hIcmpFile, ipAddress, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 2000);
您的ipaddr
为1,可能这是1.0.0
地址的来源。谢谢。现在,我可以看到ipAddress变量已正确填充,但PING似乎仍发送到1.0.0。0@aliipaddr
是InetPton
的状态返回值,而不是实际的ip地址。您应该传入什么toIcmpSendEcho
是ipAddress
,而不是有点混淆的名称ipAddress
。是的。它现在可以工作。但只在本地网络上。我有一个路由器,我想这就是为什么。即使防火墙关闭,我仍然无法从互联网PING IP。你试图PING的IP实际上是用普通网络PING成功的IPping工具?谢谢。现在,我可以看到ipAddress变量已正确填充,但ping似乎仍发送到1.0.0。0@aliipaddr
是InetPton
的状态返回值,而不是实际的ip地址。应该传递到IcmpSendEcho
的是ipAddress
,而不是有点令人困惑的namedipaddr
。是的。它现在可以工作了。但只在本地网络上工作。我有一个路由器,我想这就是原因。即使防火墙关闭,我仍然无法从互联网PING IP。你试图PING IP,但你用普通PING工具成功PING的IP?当然,这就是问题所在。ipAddress应该是ipaddrIPAddr
。Thnks