《TCP/IP网络编程》第8章

yxqq378287007 2022-01-15 02:48:38 阅读数:386

编程 网络 TCP ip

《TCP/IP网络编程》第8章

//查看域名信息
ping www.naver.com
//查看默认DNS服务器
nslookup

域名获取IP

#include <netdb.h>
//失败null
struct hostent * gethostbyname(const char *hostname);
struct hostent {

//official domain name,官方域名
char * h_name;
//alias list,同一IP可绑定多个域名
char ** h_aliases;
//host address type
int h_addrtype;
//address length
int h_length;
//address list,同一域名多个IP
char ** h_addr_list;
}

gethostbyname.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
void error_handling(char *message);
int main(int argc, char *argv[]) {

int i;
struct hostent *host;
if(argc!=2) {

printf("Usage : %s <addr>\n", argv[0]);
exit(1);
}
host = gethostbyname(argv[1]);
if(!host )
error_handling("gethostbyname... error");
printf("Official name: %s\n", host->h_name)
for(i=0; host->h_aliases[i]; i++)
printf("Aliases %d: %s\n", i+1, host->h_aliases[i]);
printf("Address type: %s\n",
host->h_addrtype==AF_INET?"AF_INET":"AF_INET6");
for(i=0; host->h_addr_list[i]; i++)
printf("IP addr %d: %s\n", i+1,
inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
return 0;
}
void error_handling(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc gethostbyname.c -o hostname
./hostname www.naver.com

IP获取域名

#include <netdb.h>
//失败null
//len,IPV4为4,IPV6为16
struct hostent * gethostbyaddr(const char *addr, socklen_t len, int family);
struct hostent {

//official domain name,官方域名
char * h_name;
//alias list,同一IP可绑定多个域名
char ** h_aliases;
//host address type
int h_addrtype;
//address length
int h_length;
//address list,同一域名多个IP
char ** h_addr_list;
}

gethostbyaddr.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
void error_handling(char *message);
int main(int argc, char *argv[]) {

int i;
struct hostent *host;
struct sockaddr_in addr;
if(argc!=2) {

printf("Usage : %s <IP>\n", argv[0]);
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = inet_addr(argv[1]);
host = gethostbyaddr((char*)&addr.sin_addr, 4, AF_INET);
if(!host)
error_handling("gethostbyaddr... error");
printf("Official name: %s\n", host->h_name)
for(i=0; host->h_aliases[i]; i++)
printf("Aliases %d: %s\n", i+1, host->h_aliases[i]);
printf("Address type: %s\n",
host->h_addrtype==AF_INET?"AF_INET":"AF_INET6");
for(i=0; host->h_addr_list[i]; i++)
printf("IP addr %d: %s\n", i+1,
inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
return 0;
}
void error_handling(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc gethostbyaddr.c -o hostaddr
./hostaddr 74.125.106

Windows实现

#include <winsock2.h>
//失败null
struct hostent * gethostbyname(const char *name);
//失败null
//len,IPV4为4,IPV6为16
struct hostent * gethostbyaddr(const char *addr, int len, int type);
struct hostent {

//official domain name,官方域名
char * h_name;
//alias list,同一IP可绑定多个域名
char ** h_aliases;
//host address type
int h_addrtype;
//address length
int h_length;
//address list,同一域名多个IP
char ** h_addr_list;
}

gethostbyname_win.c

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
void ErrorHanding(char *message);
int main(int argc, char *argv[]) {

WSADATA wsaData;
int i;
struct hostent *host;
if(argc!=2) {

printf("Usage : %s <addr>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHanding("WSAStartup() error!");
host = gethostbyname(argv[1]);
if(!host )
ErrorHanding("gethostbyname... error");
printf("Official name: %s\n", host->h_name)
for(i=0; host->h_aliases[i]; i++)
printf("Aliases %d: %s\n", i+1, host->h_aliases[i]);
printf("Address type: %s\n",
host->h_addrtype==AF_INET?"AF_INET":"AF_INET6");
for(i=0; host->h_addr_list[i]; i++)
printf("IP addr %d: %s\n", i+1,
inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
WSACleanup();
return 0;
}
void ErrorHanding(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc gethostbyname_win.c -o hostnameWin
./hostnameWin www.naver.com

gethostbyaddr_win.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
void ErrorHanding(char *message);
int main(int argc, char *argv[]) {

WSADATA wsaData;
int i;
struct hostent *host;
SOCKADDR_IN addr;
if(argc!=2) {

printf("Usage : %s <IP>\n", argv[0]);
exit(1);
}
if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
ErrorHanding("WSAStartup() error!");
memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = inet_addr(argv[1]);
host = gethostbyaddr((char*)&addr.sin_addr, 4, AF_INET);
if(!host)
ErrorHanding("gethostbyaddr... error");
printf("Official name: %s\n", host->h_name)
for(i=0; host->h_aliases[i]; i++)
printf("Aliases %d: %s\n", i+1, host->h_aliases[i]);
printf("Address type: %s\n",
host->h_addrtype==AF_INET?"AF_INET":"AF_INET6");
for(i=0; host->h_addr_list[i]; i++)
printf("IP addr %d: %s\n", i+1,
inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
WSACleanup();
return 0;
}
void ErrorHanding(char *message) {

fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
gcc gethostbyaddr_win.c -o hostaddrWin
./hostaddrWin 74.125.106
版权声明:本文为[yxqq378287007]所创,转载请带上原文链接,感谢。 https://blog.csdn.net/oqqyx1234567/article/details/122358426