zoukankan      html  css  js  c++  java
  • socket测试

    基于socket 实现daytime(13)服务器(端口我们使用13+后三位学号)和客户端
    服务器响应消息格式是

    客户端IP:XXXX
    服务器实现者学号:XXXXXXXX
    当前时间: XX:XX:XX

    上方提交代码
    提交一个客户端至少查询三次时间的截图测试截图
    提交至少两个客户端查询时间的截图测试截图

    代码如下:

    #include <stdio.h>
    #include <unistd.h>
    #include <netinet/in.h>                                                         
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <sys/wait.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define MAXLINE 200
    #define RIO_BUFSIZE 8192
    
    typedef struct{
               int rio_fd;
               int rio_cnt;
               char *rio_bufptr;
               char rio_buf[RIO_BUFSIZE];
    
    }rio_t;
    
    typedef struct sockaddr SA;
    typedef struct{
            int tm_sec;
            int tm_min;
            int tm_hour;
            int tm_mday;
            int tm_mon;
            int tm_year;
            int tm_wday;
            int tm_yday;
            int tm_isdst;
    }tm;
    
    void sigchld_handler(int sig){
    
                pid_t pid;
                int stat;
    
                while((pid = waitpid(-1,&stat,WNOHANG))>0){
                    printf("child %d terminated\n",pid);
     }
                return;
    }
    
    int main(int argc,char **argv){
    
                int listenfd,connfd,port,clientlen;
                struct sockaddr_in clientaddr;
                struct hostent *hp;
                char *haddrp;
                char sbuf[MAXLINE];
                char rbuf[MAXLINE];
                rio_t rio;
                time_t lt;
                tm *local;
                char str1[MAXLINE]="客户端IP:";
                char str2[MAXLINE]="服务器实现者学号:";
                char str3[MAXLINE]="当地时间:";
    
        if(argc != 2){
    
                fprintf(stderr,"usage:%s <port>\n",argv[0]);
                exit(0);
                                                                            }
          port = atoi(argv[1]);
    signal(SIGCHLD,sigchld_handler);
        listenfd = open_listenfd(port);
                                                                                while(1){
    
         clientlen = sizeof(clientaddr);
         connfd = accept(listenfd,(SA *)&clientaddr,&clientlen);
         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)\n",hp->h_name,haddrp);
         if(fork() == 0){
         close(listenfd);
         lt = time(NULL);
         local = localtime(&lt);
         strftime(sbuf,64,"%Y-%m-%d %H:%M:%S",local);
         send(connfd,sbuf,MAXLINE,0);
         close(connfd);
         exit(0);
            }
        close(connfd);
    
          }
    }
    
    
    #include <stdio.h>
    #include <netinet/in.h>                                                         
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define RIO_BUFSIZE 8192
    
    typedef struct{
        int rio_fd;
        int rio_cnt;
        char *rio_bufptr;
        char rio_buf[RIO_BUFSIZE];
    
    }rio_t;
    
    #define MAXLINE 200
    
    int main(int argc,char **argv){
    int clientfd,port;
        char *host,buf[MAXLINE];
        char sbuf[MAXLINE];
        char rbuf[MAXLINE];
        rio_t rio;
        char str1[MAXLINE]="客户端IP:";
        char str2[MAXLINE]="学号:20191316";
    
        char str3[MAXLINE]="当地时间:";
    
        if(argc!=3){
    
            fprintf(stderr,"usage:%s <host> <port>\n",argv[0]);
            exit(0);
        }
        host = argv[1];
        port = atoi(argv[2]);
    
        clientfd = open_clientfd(host,port);
    while(1){
    
    
            recv(clientfd,rbuf,MAXLINE,0);
    
            printf("%s",str1);
            puts(host);
    
            printf("%s",str2);
            putchar('\n');
    
            printf("%s",str3);
    
            puts(rbuf);
    
            close(clientfd);
    
            exit(0);
        }
    }
    

    因为我虚拟机的时间和标准时间不对应,老师可以看我截图下方本机的时间

    两个客户端

  • 相关阅读:
    错误
    分页查询
    异步请求jquery
    深入理解C/C++ [Deep C (and C++)]
    C语言经典算法100例(三)
    《Python》 计算机基础
    Python程序员的进化史
    以前没有写笔记的习惯,现在慢慢的发现及时总结是多么的重要。 这一篇文章主要关于java多线程一些常见的疑惑点。因为讲解多线程的书籍和文章已经很多了,所以我也不好意思多说,嘻嘻嘻、大家可以去参考一些那些书籍。我这个文章主要关于实际的一些问题。同时也算是我以后复习的资料吧,。还请大家多多指教。 同时希望多结交一些技术上的朋友。谢谢。
    快速读入函数
    一元二次方程公式
  • 原文地址:https://www.cnblogs.com/ffffatal/p/15710266.html
Copyright © 2011-2022 走看看