• 2017-2018-1 20155223 实验三 实时系统


    2017-2018-1 20155223 实验三 实时系统

    实验1

    学习使用Linux命令wc(1)
    基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端
    客户端传一个文本文件给服务器
    服务器返加文本文件中的单词数

    wc命令:Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。
    那么就是要我建立一对C/S,客户端可以发送文件,服务器读里面的单词个数。

    #include <stdlib.h>
    
    #include <stdio.h>
    
    #include <errno.h>
    
    #include <string.h>
    
    #include <unistd.h>
    
    #include <netdb.h>
    
    #include <sys/socket.h>
    
    #include <netinet/in.h>
    
    #include <sys/types.h>
    
    #include <arpa/inet.h>
    
    #include <fcntl.h>
    
    
    
    // 定义包的大小为512KB
    
    #define PACK_SIZE 1024*512
    
    
    
    int main(int argc, char *argv[])
    
    {
    
        // 设置输出缓冲
    
        setvbuf(stdout, NULL, _IONBF, 0);
    
        fflush(stdout);
    
    
    
        int sockfd,new_fd;
    
        struct sockaddr_in server_addr;
    
        struct sockaddr_in client_addr;
    
        int sin_size,portnumber;
    
        char hello[]="Hello! Are You Fine?
    ";
    
    
    
        if((portnumber=atoi("155207"))<0)
    
        {
    
            fprintf(stderr,"Usage:%s portnumbera
    ",argv[0]);
    
            exit(1);
    
        }
    
    
    
        /* 服务器端开始建立socket描述符 */
    
        if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) {
    
            fprintf(stderr,"Socket error:%s
    a",strerror(errno));
    
            exit(1);
    
        }
    
    
    
        /* 服务器端填充 sockaddr结构  */
    
        bzero(&server_addr,sizeof(struct sockaddr_in));
    
        server_addr.sin_family=AF_INET;
    
        server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    
        server_addr.sin_port=htons(portnumber);
    
    
    
        /* 捆绑sockfd描述符  */
    
        if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) {
    
            fprintf(stderr,"Bind error:%s
    a",strerror(errno));
    
            exit(1);
    
        }
    
    
    
        /* 监听sockfd描述符  */
    
        if(listen(sockfd,5)==-1) {
    
            fprintf(stderr,"Listen error:%s
    a",strerror(errno));
    
            exit(1);
    
        }
    
    
    
        while(1)
    
        {
    
            fprintf(stderr, "server is listening!
    ");
    
    
    
            /* 服务器阻塞,直到客户程序建立连接  */
    
            sin_size=sizeof(struct sockaddr_in);
    
            if( ( new_fd = accept(sockfd,(struct sockaddr *)(&client_addr),(socklen_t*)&sin_size ) ) == -1) {
    
                fprintf(stderr,"Accept error:%s
    a",strerror(errno));
    
                exit(1);
    
            }
    
    
    
            fprintf(stderr,"Server get connection from %s
    ",
    
                inet_ntoa(client_addr.sin_addr));
    
            if(write(new_fd,hello,strlen(hello))==-1) {
    
                fprintf(stderr,"Write Error:%s
    ",strerror(errno));
    

    实验2

    实验二
    使用多线程实现wc服务器并使用同步互斥机制保证计数正确
    多线程就是一台服务器能够响应多台客户端的要求。

    #include <stdlib.h>
    
    #include <stdio.h>
    
    #include <errno.h>
    
    #include <string.h>
    
    #include <unistd.h>
    
    #include <netdb.h>
    
    #include <sys/socket.h>
    
    #include <netinet/in.h>
    
    #include <sys/types.h>
    
    #include <arpa/inet.h>
    
    #include <fcntl.h>
    
    
    
    // 定义包的大小为512KB
    
    #define PACK_SIZE 1024*512
    
    
    
    int main(int argc, char *argv[])
    
    {
    
        // 设置输出缓冲
    
        setvbuf(stdout, NULL, _IONBF, 0);
    
        fflush(stdout);
    
    
    
        int sockfd,new_fd;
    
        struct sockaddr_in server_addr;
    
        struct sockaddr_in client_addr;
    
        int sin_size,portnumber;
    
        char hello[]="Hello! Are You Fine?
    ";
    
    
    
        if((portnumber=atoi("155207"))<0)
    
        {
    
            fprintf(stderr,"Usage:%s portnumbera
    ",argv[0]);
    
            exit(1);
    
        }
    
    
    
        /* 服务器端开始建立socket描述符 */
    
        if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) {
    
            fprintf(stderr,"Socket error:%s
    a",strerror(errno));
    
            exit(1);
    
        }
    
    
    
        /* 服务器端填充 sockaddr结构  */
    
        bzero(&server_addr,sizeof(struct sockaddr_in));
    
        server_addr.sin_family=AF_INET;
    
        server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    
        server_addr.sin_port=htons(portnumber);
    
    
    
        /* 捆绑sockfd描述符  */
    
        if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) {
    
            fprintf(stderr,"Bind error:%s
    a",strerror(errno));
    
            exit(1);
    
        }
    
    
    
        /* 监听sockfd描述符  */
    
        if(listen(sockfd,5)==-1) {
    
            fprintf(stderr,"Listen error:%s
    a",strerror(errno));
    
            exit(1);
    
        }
    
    
    
        while(1)
    
        {
    
            fprintf(stderr, "server is listening!
    ");
    
    
    
            /* 服务器阻塞,直到客户程序建立连接  */
    
            sin_size=sizeof(struct sockaddr_in);
    
            if( ( new_fd = accept(sockfd,(struct sockaddr *)(&client_addr),(socklen_t*)&sin_size ) ) == -1) {
    
                fprintf(stderr,"Accept error:%s
    a",strerror(errno));
    
                exit(1);
    
            }
    
    
    
            fprintf(stderr,"Server get connection from %s
    ",
    
                inet_ntoa(client_addr.sin_addr));
    
            if(write(new_fd,hello,strlen(hello))==-1) {
    
                fprintf(stderr,"Write Error:%s
    ",strerror(errno));
    

  • 相关阅读:
    为服务部署 Jekins的使用
    spring cloud
    docker
    WebSocket
    idea
    maven
    SQL四种语言(DDL、 DML、 DCL、 TCL)
    十大经典排序
    AVL树的旋转图解和简单实现
    多个线程交替打印
  • 原文地址:https://www.cnblogs.com/lnaswxc/p/7859330.html
Copyright © 2020-2023  润新知