sockets-getdate-udp.c

来源:互联网 发布:淘宝店铺评分计算器 编辑:程序博客网 时间:2024/05/08 06:48
[root@localhost chapter15]# ./getdate-udp daytime port is 13read 26 bytes: 28 AUG 2011 23:35:09 PDT[root@localhost chapter15]# cat getdate-udp.c /*  Start with the usual includes and declarations.  */#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>int main(int argc, char *argv[]){    char *host;    int sockfd;    int len, result;    struct sockaddr_in address;    struct hostent *hostinfo;    struct servent *servinfo;    char buffer[128];    if(argc == 1)        host = "localhost";    else        host = argv[1];/*  Find the host address and report an error if none is found.  */    hostinfo = gethostbyname(host);    if(!hostinfo) {        fprintf(stderr, "no host: %s\n", host);        exit(1);    }/*  Check that the daytime service exists on the host.  */    servinfo = getservbyname("daytime", "udp");    if(!servinfo) {        fprintf(stderr,"no daytime service\n");        exit(1);    }    printf("daytime port is %d\n", ntohs(servinfo -> s_port));/*  Create a UDP socket.  */    sockfd = socket(AF_INET, SOCK_DGRAM, 0);/*  Construct the address for use with sendto/recvfrom...  */    address.sin_family = AF_INET;    address.sin_port = servinfo -> s_port;    address.sin_addr = *(struct in_addr *)*hostinfo -> h_addr_list;    len = sizeof(address);    result = sendto(sockfd, buffer, 1, 0, (struct sockaddr *)&address, len);    result = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&address, &len);    buffer[result] = '\0';    printf("read %d bytes: %s", result, buffer);    close(sockfd);    exit(0);} 
[root@localhost chapter15]# ./getdate-udp daytime port is 13read 26 bytes: 29 AUG 2011 11:36:41 PD
tcp  vs    udp
read-sendto
write-recvfrom


原创粉丝点击