#include <Winsock2.h>
#include <stdio.h>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment (lib, "Ws2_32.lib")
int main(void)
{
printf("%d\n", htons(80));
printf("%d\n", ntohs(20480));
printf("%d\n", htonl(649189134));
WORD wVersion;
WSADATA WSAData;
wVersion = MAKEWORD(2, 2);
WSAStartup(wVersion, &WSAData);
struct addrinfo hints;
struct addrinfo *res, *cur;
int ret;
struct sockaddr_in *addr;
char m_ipaddr[16];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; /* Allow IPv4 */
hints.ai_flags = AI_PASSIVE;/* For wildcard IP address */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_socktype = SOCK_STREAM;
ret = getaddrinfo("www.baidu.com", NULL, &hints, &res);
if (ret == -1) {
perror("getaddrinfo");
exit(1);
}
for (cur = res; cur != NULL; cur = cur->ai_next) {
addr = (struct sockaddr_in *)cur->ai_addr;
sprintf(m_ipaddr, "%d.%d.%d.%d",
(*addr).sin_addr.S_un.S_un_b.s_b1,
(*addr).sin_addr.S_un.S_un_b.s_b2,
(*addr).sin_addr.S_un.S_un_b.s_b3,
(*addr).sin_addr.S_un.S_un_b.s_b4);
printf("getaddrinfo:%s\n", m_ipaddr);
}
freeaddrinfo(res);
hostent* remoteHost = gethostbyname("www.baidu.com");
if (remoteHost)
{
if (remoteHost->h_addrtype == AF_INET)
{
int i = 0;
in_addr addr;
while (remoteHost->h_addr_list[i] != 0)
{
addr.S_un.S_addr = *(u_long *)remoteHost->h_addr_list[i++];
printf("gethostbyname:IP Address #%d: %s\n", i, inet_ntoa(addr));
}
}
}
getchar();
return 0;
}
域名获取IP
最新推荐文章于 2024-08-08 10:51:06 发布