zoukankan      html  css  js  c++  java
  • 第八周测试3

    客户端:
    #include "csapp.h"
    int main(int argc, char **argv)
    {
        int clientfd, port;
        char *host, buf[MAXLINE];
        rio_t rio;
        if (argc != 3) {
            fprintf(stderr, "usage: %s <host> <port>
    ", argv[0]);
            exit(0);
        }
        host = argv[1];
        port = atoi(argv[2]);
        clientfd = Open_clientfd(host, port);
        Rio_readinitb(&rio, clientfd);
        while (Fgets(buf, MAXLINE, stdin) != NULL) {
            Rio_writen(clientfd, buf, strlen(buf));
            Rio_readlineb(&rio, buf, MAXLINE);
            Fputs(buf, stdout);
        }
        Close(clientfd);
        exit(0);
    }
    服务端:
    #include <stdio.h>
    #include <stdlib.h>
    #include "csapp.h"
    #include <time.h>
    void echo(int connfd);
    int main(int argc, char **argv) 
    {
        int listenfd, connfd, port, clientlen;
        struct sockaddr_in clientaddr;
        struct hostent *hp;
        char *haddrp;
        if (argc != 2) {
        fprintf(stderr, "usage: %s <port>
    ", argv[0]);
        exit(0);
        }
        port = atoi(argv[1]);
        listenfd = Open_listenfd(port);
        while (1) {
        clientlen = sizeof(clientaddr);
        connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
        /* determine the domain name and IP address of the client */
        hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, 
                   sizeof(clientaddr.sin_addr.s_addr), AF_INET);
        haddrp = inet_ntoa(clientaddr.sin_addr);
        printf("server connected to %s (%s)
    ", hp->h_name, haddrp);
        echo(connfd);
        Close(connfd);
        }
        exit(0);
    }
    void echo(int connfd)
    {
        size_t n;
        char buf[MAXLINE];
        rio_t rio;
        
        Rio_readinitb(&rio, connfd);
        while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) {
            printf("客户端IP:127.0.0.1
    ");
            printf("服务器实现学号:20155230
    ");
            printf("server received %d bytes
    ", n);
            time_t t;
            time(&t);
            printf("当前时间:%s
    ",ctime(&t));
            Rio_writen(connfd, buf, n);
        }
    }

     

  • 相关阅读:
    第二周
    7月课程
    自定义线程池的创建
    jvm8内存模型和内存分配
    多线程中 CountDownLatch CyclicBarrier Semaphore的使用
    HashMap Hashtable TreeMap LinkedHashMap 分析
    HashSet LinkedHashSet TreeSet 分析
    ArrayList Vector LinkedList分析
    Java之流水号生成器实现
    mysql-mmm 部署高可用集群
  • 原文地址:https://www.cnblogs.com/fakerli/p/11939725.html
Copyright © 2011-2022 走看看