• TCP时间获取程序


    一、服务器程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <sys/socket.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    #define MAXLINE 4096
    #define LISTENQ 1024
    #define SA struct sockaddr
    
    int main(int argc, char *argv[]) {
        int listenfd, connfd;
        struct sockaddr_in servaddr;
        char buff[MAXLINE];
        time_t ticks;
    
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
    
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(9999);
    
        bind(listenfd, (SA *)&servaddr, sizeof(servaddr));
    
        listen(listenfd, LISTENQ);
    
        for ( ; ; ) {
            connfd = accept(listenfd, (SA *)NULL, NULL);
    
            ticks = time(NULL);
            snprintf(buff, sizeof(buff), "%.24s
    ", ctime(&ticks));
            write(connfd, buff, strlen(buff));
    
            close(connfd);
        }
        exit(0);
    }

    二、显示客户IP地址和端口号的服务器程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <sys/socket.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    
    #define MAXLINE 4096
    #define LISTENQ 1024
    #define SA struct sockaddr
    
    int main(int argc, char *argv[]) {
        int listenfd, connfd;
        socklen_t len;
        struct sockaddr_in servaddr, cliaddr;
        char buff[MAXLINE];
        time_t ticks;
    
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
    
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(9999);
    
        bind(listenfd, (SA *)&servaddr, sizeof(servaddr));
    
        listen(listenfd, LISTENQ);
    
        for ( ; ; ) {
            len = sizeof(cliaddr);
            connfd = accept(listenfd, (SA *)&cliaddr, &len);
            printf("connection from %s, port %d
    ", inet_ntop(AF_INET, 
                &cliaddr.sin_addr, buff, sizeof(buff)), ntohs(cliaddr.sin_port));
    
            ticks = time(NULL);
            snprintf(buff, sizeof(buff), "%.24s
    ", ctime(&ticks));
            write(connfd, buff, strlen(buff));
    
            close(connfd);
        }
        exit(0);
    }

    三、并发服务器程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <sys/socket.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    #define MAXLINE 4096
    #define LISTENQ 1024
    #define SA struct sockaddr
    
    int main(int argc, char *argv[]) {
        int listenfd, connfd;
        pid_t pid;
        struct sockaddr_in servaddr;
        char buff[MAXLINE];
        time_t ticks;
    
        listenfd = socket(AF_INET, SOCK_STREAM, 0);
    
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(9999);
    
        bind(listenfd, (SA *)&servaddr, sizeof(servaddr));
    
        listen(listenfd, LISTENQ);
    
        for ( ; ; ) {
            connfd = accept(listenfd, (SA *)NULL, NULL);
    
            if ( (pid = fork()) == 0) {
                close(listenfd);
    
                ticks = time(NULL);
                snprintf(buff, sizeof(buff), "%.24s
    ", ctime(&ticks));
                write(connfd, buff, strlen(buff));
    
                close(connfd);
            }
    
            close(connfd);
        }
        exit(0);
    }

    四、客户端程序

    #include <stdio.h>
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    
    #define SA struct sockaddr
    #define MAXLINE 4096
    
    int main(int argc, char *argv[]) {
        int sockfd, n;
        char recvline[MAXLINE + 1];
        struct sockaddr_in servaddr;
        
        if (argc != 2) {
            printf("usage: ./daytimetcpcli <IPaddress>
    ");
            return 0;
        }    
        if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
            perror("socket");
            return 0;
        }
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(9999);
        if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
            perror("inet_pton");
            return 0;
        }
        if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) {
            perror("connect");
        }
        while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
            recvline[n] = 0;
            if (fputs(recvline, stdout) == EOF) {
                perror("fputs");
            }
        }
        if (n < 0) {
            perror("read");
        }
        exit(0);
    }

     

  • 相关阅读:
    phpize使用方法
    PHP安装redis扩展
    lnmp 搭建后,nginx下php文件404但是html文件正常访问
    nginx配置https访问安装ssl证书
    Linux安装git最新版本
    微信公众号开发处理微信昵称带图片问题
    如何撰写技术文档
    Asp.Net Core 2.0实现HttpResponse中繁切换
    EF6.0 下sql语句自动生成的参数类型decimal(18,2)修改
    Windows注册表详解
  • 原文地址:https://www.cnblogs.com/soldierback/p/10669743.html
Copyright © 2020-2023  润新知