本文为senlie原创。转载请保留此地址:
/*** UDP。协议无关,调用 getaddrinfo 和 udp_client**/#include "unp.h"intudp_client(const char *host, const char *serv, SA **saptr, socklen_t *lenp){ int sockfd, n; struct addrinfo hints, *res, *ressave; //1.调用 getaddrinfo //协议地址族为 AF_UNSPEC ,套接字类型为 SOCK_DGRAM bzero(&hints, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) err_quit("udp_client error for %s, %s: %s", host, serv, gai_strerror(n)); ressave = res; //2.尝试每一个 addrinfo 结构直到成功或到达链表尾 do { // 创建套接字 sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (sockfd >= 0) break; /* success */ } while ( (res = res->ai_next) != NULL); //3.假设 getaddrinfo 失败或创建套接字不成功。本函数将终止 if (res == NULL) /* errno set from final socket() */ err_sys("udp_client error for %s, %s", host, serv); //4.保存server套接字地址结构 *saptr = Malloc(res->ai_addrlen); memcpy(*saptr, res->ai_addr, res->ai_addrlen); *lenp = res->ai_addrlen; //5.调用 freeaddrinfo 清理由 getaddrinfo 返回的动态存储空间 freeaddrinfo(ressave); //6.返回套接字 return(sockfd);}/* end udp_client */intUdp_client(const char *host, const char *serv, SA **saptr, socklen_t *lenptr){ return(udp_client(host, serv, saptr, lenptr));}
/*** UDP,协议无关。调用 getaddrinfo 和 udp_client**/#include "unp.h"intmain(int argc, char **argv){ int sockfd, n; char recvline[MAXLINE + 1]; socklen_t salen; struct sockaddr *sa; if (argc != 3) err_quit("usage: daytimeudpcli1"); //1.利用 Udp_client 获得 UDP 套接字和server套接字地址结构 sockfd = Udp_client(argv[1], argv[2], (void **) &sa, &salen); //2.显示server地址 printf("sending to %s\n", Sock_ntop_host(sa, salen)); //3.发送1字节的数据报 Sendto(sockfd, "", 1, 0, sa, salen); /* send 1-byte datagram */ //4.读取并显示应答数据报 n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); recvline[n] = '\0'; /* null terminate */ Fputs(recvline, stdout); exit(0);}