C语言IP地址查询系统如何实现?
发布网友
发布时间:2022-03-02 08:41
我来回答
共3个回答
热心网友
时间:2022-03-02 10:10
我觉得你这个需求,倒不一定非得用c语言来实现。你这个其实可以调用百度的插件,因为你在百度上输入这个IP地址的话,然后他就能查出来这个IP所在的位置,你可以用JAVA,然后调用百度的一些插件就能实现这个功能。
热心网友
时间:2022-03-02 11:28
C语言如何实现IP地址查询系统
参考如下:
void GetNameAndIp()
{
struct hostent *host;
struct in_addr *ptr;
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum(dwScope, NULL, NULL, NULL, &hEnum);
WSADATA wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
if (hEnum)
{
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 10240;
LPVOID Buffer = new char[10240];
WNetEnumResource(hEnum, &Count, Buffer, &BufferSize);
NetResource = (NETRESOURCE*)Buffer;
char szHostName[200];
for (unsigned int i = 0; i < BufferSize / sizeof(NETRESOURCE); i++, NetResource++)
{
if (NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY)
{
if (NetResource->lpRemoteName)
{
CString strFullName = NetResource->lpRemoteName;
if (0 == strFullName.Left(2).Compare(_T("\\\\")))
strFullName = strFullName.Right(strFullName.GetLength() - 2);
gethostname(szHostName, strlen(szHostName));
USES_CONVERSION;
char *pchar = T2A(strFullName);
host = gethostbyname(pchar);
if (host == NULL) continue;
ptr = (struct in_addr *) host->h_addr_list[0];
string str = "";
for (int n = 0; n<4; n++)
{
CString addr;
if (n > 0)
{
str += ".";
}
int value = (unsigned int)((unsigned char*)host->h_addr_list[0])[n];
char p[20];
sprintf(p, "%d", value);
str.append(p);
}
std::cout <<"IP:"<< str <<" Name:"<<host->h_name<< std::endl;
}
}
}
delete Buffer;
WNetCloseEnum(hEnum);
}
WSACleanup();
}
热心网友
时间:2022-03-02 13:03
我想问,你现在写出来了吗?