五十七、linux 编程——UDP 编程 域名解析

时间:2020-12-23 21:19:28

57.1 介绍

57.1.1 域名解析

  五十七、linux 编程——UDP 编程 域名解析

57.1.2 域名解析函数

  五十七、linux 编程——UDP 编程 域名解析

  gethostent 可以获取多组,gethostbyname 只可以获取一组

  /etc/hosts 文件设置了域名和 IP 的绑定关系

  五十七、linux 编程——UDP 编程 域名解析

  五十七、linux 编程——UDP 编程 域名解析

57.2 例子

57.2.1 例子1

  gethost.c

 #include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <arpa/inet.h> void out_addr(struct hostent *h)
{
printf("hostname: %s\n", h->h_name);
printf("addrtype: %s\n", h->h_addrtype == AF_INET ? "IPV4": "IPV6");
char ip[];
memset(ip, , sizeof(ip));
inet_ntop(h->h_addrtype, h->h_addr_list[], ip, sizeof(ip));
printf("ip address: %s\n", ip); int i = ;
while(h->h_aliases[i] != NULL){
printf("aliase: %s\n", h->h_aliases[i]);
i++;
}
} int main(int argc, char *argv[])
{
if(argc < ){
printf("usage: %s host\n", argv[]);
exit();
} struct hostent *h;
h = gethostbyname(argv[]);
if( NULL != h){
out_addr(h);
}
else{
perror("get hostbyname error");
} return ;
}

  修改下 /etc/hosts 文件,给 localhost 增加一个别名

  五十七、linux 编程——UDP 编程 域名解析

  编译运行:

  五十七、linux 编程——UDP 编程 域名解析

  gethostbyname 有一些缺陷,支持 IPV4 但是不支持 IPV6,且不能用于多线程中。

57.2.2 例子2

  gethost2.c

 #include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <arpa/inet.h> void out_addr(struct hostent *h)
{
printf("hostname: %s\n", h->h_name);
printf("addrtype: %s\n", h->h_addrtype == AF_INET ? "IPV4": "IPV6");
char ip[];
memset(ip, , sizeof(ip));
inet_ntop(h->h_addrtype, h->h_addr_list[], ip, sizeof(ip));
printf("ip address: %s\n", ip); int i = ;
while(h->h_aliases[i] != NULL){
printf("aliase: %s\n", h->h_aliases[i]);
i++;
}
} int main(int argc, char *argv[])
{
if(argc < ){
fprintf(stderr, "usage: %s host\n", argv[]);
exit();
} struct hostent *h;
while((h = gethostent()) != NULL){
if(!strcmp(argv[], h->h_name)){
out_addr(h);
exit();
}
else{
int i = ;
while(h->h_aliases[i] != NULL){
if(!strcmp(argv[], h->h_aliases[i])){
out_addr(h);
exit();
}
i++;
}
}
} endhostent();
printf("no %s exist\n", argv[]); return ;
}

  编译运行:

  五十七、linux 编程——UDP 编程 域名解析

57.2.3 例子3

  修改前面 time_udp_client.c 文件

 #include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <time.h> int is_host(struct hostent *host, char *name)
{
if(!strcmp(host->h_name, name)) return ;
int i = ;
while(host->h_aliases[i] != NULL){
if(!strcmp(host->h_aliases[i], name)) return ;
i++;
} return ;
} unsigned int get_ip_by_name(char *name)
{
unsigned int ip = ;
struct hostent *host;
while((host = gethostent()) != NULL){
if(is_host(host, name)){
memcpy(&ip, host->h_addr_list[], );
break;
}
} endhostent();
return ip;
} int main(int argc, char *argv[])
{
if(argc < ){
printf("usage: %s ip port\n", argv[]);
exit();
} /** 步骤1: 创建 socket */
int sockfd = socket(AF_INET, SOCK_DGRAM, );
if(sockfd < ){
perror("socket error");
exit();
} /** 步骤2: 调用 recvfrom 和 sendto 等函数和服务器端双向通信 */
struct sockaddr_in serveraddr;
memset(&serveraddr, , sizeof(serveraddr));
serveraddr.sin_family = AF_INET; ///< ipv4
serveraddr.sin_port = htons(atoi(argv[2])); ///< port unsigned int ip = get_ip_by_name(argv[]);
if(ip != ){
serveraddr.sin_addr.s_addr = ip;
}
else {
inet_pton(AF_INET, argv[], &serveraddr.sin_addr.s_addr);
} char buffer[] = "hello world";
/** 向服务器端发送数据报文 */
if(sendto(sockfd, buffer, sizeof(buffer), , (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < ){
perror("sendto error");
exit();
}
else{
/** 接受服务器端发送的数据报文 */
memset(buffer, , sizeof(buffer));
if(recv(sockfd, buffer, sizeof(buffer), ) < ){
perror("recv error");
exit();
}
else{
printf("%s", buffer);
}
} close(sockfd); return ;
}

  编译,运行服务器和客户端测试:

  五十七、linux 编程——UDP 编程 域名解析