我在收集新闻信息。我从msdn得到的apis很少,而且运行得很成功。我想收集联网pcs的所有IP地址。到现在为止,这是我得到的输出。我没有为IP地址实现任何API/函数。有人能帮上忙吗。
平台: 69639名称: GSI1版本: 6.2类型: 500 IP地址:
平台: 69635名称: HELLO-PC版本: 6.1类型: 500 IP地址:
平台: 331779名称: SCP版本:6.3型: 500 IP地址:
平台: 200711名称: SCP-PC版本: 6.1类型: 500 IP地址:
发布于 2014-03-29 13:55:50
您可以使用此代码检索10个IP地址:
#include <winsock2.h>
// Add 'ws2_32.lib' to your linker options
WSADATA WSAData;
// Initialize winsock dll
if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
{
// Error handling
}
// Get local host name
char szHostName[128] = "";
if(::gethostname(szHostName, sizeof(szHostName)))
{
// Error handling -> call 'WSAGetLastError()'
}
// Get local IP addresses
struct sockaddr_in SocketAddress;
struct hostent *pHost = 0;
pHost = ::gethostbyname(szHostName);
if(!pHost)
{
// Error handling -> call 'WSAGetLastError()'
}
char aszIPAddresses[10][16]; // maximum of ten IP addresses
for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
{
memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
}
// Cleanup
WSACleanup();您可以找到其他示例Here。
https://stackoverflow.com/questions/22727501
复制相似问题