⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getdate.c

📁 linux的程序设计第二版 是学习LINUX下程序设计的好教程哦
💻 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>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", "tcp");    if(!servinfo) {        fprintf(stderr,"no daytime service\n");        exit(1);    }    printf("daytime port is %d\n", ntohs(servinfo -> s_port));/*  Create a socket.  */    sockfd = socket(AF_INET, SOCK_STREAM, 0);/*  Construct the address for use with connect...  */    address.sin_family = AF_INET;    address.sin_port = servinfo -> s_port;    address.sin_addr = *(struct in_addr *)*hostinfo -> h_addr_list;    len = sizeof(address);/*  ...then connect and get the information.  */    result = connect(sockfd, (struct sockaddr *)&address, len);    if(result == -1) {        perror("oops: getdate");        exit(1);    }    result = read(sockfd, buffer, sizeof(buffer));    buffer[result] = '\0';    printf("read %d bytes: %s", result, buffer);    close(sockfd);    exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -