2019-2020 20175207- 20175235 实验三 实时系统

20175207-20175235 实验三 实时系统

20175207冷南
20175235泽仁拉措

目录:

并发程序-1

并发程序-2

任务详情
学习使用Linux命令wc(1)
基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端
客户端传一个文本文件给服务器
服务器返加文本文件中的单词数
上方提交代码
附件提交测试截图,至少要测试附件中的两个文件

  • 实验代码:
    • 服务器端
     #include <stdio.h>
     #include <sys/types.h>
     #include <sys/socket.h>
     #include <netinet/in.h>
     #include <arpa/inet.h>
     #include <string.h>
    
     #define MYPORT 175235
    
     void main(){
     int serverfd, clientfd;
     struct sockaddr_in my_addr;
     struct sockaddr_in remote_addr;
    
     char buffer[BUFSIZ];
     memset(&my_addr, 0, sizeof(my_addr));
     my_addr.sin_family=AF_INET;
     my_addr.sin_addr.s_addr=INADDR_ANY;
     my_addr.sin_port=htons(MYPORT);
    
     if((serverfd=socket(PF_INET, SOCK_STREAM, 0))==-1){
         perror("socket");
     }
     
     if(bind(serverfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))<0){
         perror("bind");
     }
     listen(serverfd, 5);
     int addrlen=sizeof(struct sockaddr_in);
     while(1){
         if((clientfd=accept(serverfd, (struct sockaddr *)&remote_addr, &addrlen))<0){
             perror("accept");
     }
     printf("accept client %s\n", inet_ntoa(remote_addr.sin_addr));
     int len, i;
     long wordscount=0;
     int flag=1;
     while(1){
         if((len=recv(clientfd, buffer, 1024, 0))>0){
             for(i=0; i<len; i++){
                 if(flag==0){
                     switch(buffer[i]){
                         case ' ':
                             wordscount++;
                             break;
                         case '\n':
                             wordscount++;
                             break;
                         case '\r':
                             wordscount++;
                             break;
                         default:
                             break;
                     }
                 }
             if(buffer[i]== ' ' || buffer[i]=='\n' || buffer[i]=='\r') flag=1;
             else flag=0;
             }
         }
         if(len<1024) break;
     }
     send(clientfd, &wordscount, sizeof(long), 0);
     close(clientfd);
     }
     close(serverfd);
    

}

   *  客户端
    ```
    #include <stdio.h>  
    #include <sys/types.h>  
    #include <sys/socket.h>  
    #include <netinet/in.h>  
    #include <arpa/inet.h>  
    #include <string.h>

    #define MYPORT 175235

    void main(){
    int clientfd;
    struct sockaddr_in remote_addr;
    char buffer[BUFSIZ];
    memset(&remote_addr, 0 , sizeof(remote_addr));
    remote_addr.sin_family=AF_INET;
    remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    remote_addr.sin_port=htons(MYPORT);

    if((clientfd=socket(PF_INET,SOCK_STREAM,0))<0){  
        perror("socket");  
    }

    if(connect(clientfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr))<0){
        perror("connect");
    }

    int len;
    FILE *fp;
    char path[20];
    gets(path);
    fp=fopen(path, "r");
    char readch;
    int i=0;
    while((readch=fgetc(fp))!=EOF){
        if(i<1024){
            buffer[i]=readch;
            i++;
        }
        else{
            i=0;
            int n=send(clientfd, buffer, 1024, 0);
        }
    }
    fclose(fp);
    if(i!=0) 
        send(clientfd, buffer, i, 0);
    long wordscount;
    recv(clientfd, &wordscount, sizeof(long), 0);
    printf("%ld\n", wordscount);
    close(clientfd);
}


返回目录

任务详情
使用多线程实现wc服务器并使用同步互斥机制保证计数正确
上方提交代码
下方提交测试
对比单线程版

实验代码:

  • 服务器端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>

#define MYPORT 175235
pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
int serverfd, clientfd;
char buffer[BUFSIZ];

void *wc(void *m){
    pthread_mutex_lock( &counter_mutex );
    int len, i;
    long wordscount=0;
    int flag=1;
    while(1){
        if((len=recv(clientfd, buffer, 1024, 0))>0){
            for(i=0; i<len; i++){
                if(flag==0){
                    switch(buffer[i]){
                        case ' ':
                            wordscount++;
                            break;
                        case '\n':
                            wordscount++;
                            break;
                        case '\r':
                            wordscount++;
                            break;
                        default:
                            break;
                    }
                }
                if(buffer[i]== ' ' || buffer[i]=='\n' || buffer[i]=='\r') 
                    flag=1;
                else 
                    flag=0;
            }
        }
        if(len<1024) 
            break;
    }
    send(clientfd, &wordscount, sizeof(long), 0);
    close(clientfd);
    pthread_mutex_unlock( &counter_mutex );
    return NULL;
}
void main(){
    pthread_t t;
    char arg[30];

    struct sockaddr_in my_addr;
    struct sockaddr_in remote_addr;

    memset(&my_addr, 0, sizeof(my_addr));
    my_addr.sin_family=AF_INET;
    my_addr.sin_addr.s_addr=INADDR_ANY;
    my_addr.sin_port=htons(MYPORT);

    if((serverfd=socket(PF_INET, SOCK_STREAM, 0))==-1){
        perror("socket");
    }

    if(bind(serverfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))<0){
        perror("bind");
    }
    listen(serverfd, 5);
    int addrlen=sizeof(struct sockaddr_in);
    while(1){
        if((clientfd=accept(serverfd, (struct sockaddr *)&remote_addr, &addrlen))<0){
            perror("accept");
        }
        printf("accept client %s\n", inet_ntoa(remote_addr.sin_addr));
        pthread_create(&t, NULL, &wc, NULL);
        pthread_join(&t, NULL);
    }
    close(serverfd);
}
  • 客户端
#include <stdio.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <string.h>

#define MYPORT 175235

void main(){
    int clientfd;
    struct sockaddr_in remote_addr;
    char buffer[BUFSIZ];
    memset(&remote_addr, 0 , sizeof(remote_addr));
    remote_addr.sin_family=AF_INET;
    remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    remote_addr.sin_port=htons(MYPORT);

    if((clientfd=socket(PF_INET,SOCK_STREAM,0))<0){  
        perror("socket");  
    }

    if(connect(clientfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr))<0){
        perror("connect");
    }

    int len;
    FILE *fp;
    char path[20];
    gets(path);
    fp=fopen(path, "r");
    char readch;
    int i=0;
    while((readch=fgetc(fp))!=EOF){
        if(i<1024){
            buffer[i]=readch;
            i++;
        }
        else{
            i=0;
            int n=send(clientfd, buffer, 1024, 0);
        }
    }
    fclose(fp);
    if(i!=0) 
        send(clientfd, buffer, i, 0);
    long wordscount;
    recv(clientfd, &wordscount, sizeof(long), 0);
    printf("%ld\n", wordscount);
    close(clientfd);
}


返回目录

posted @ 2019-11-24 19:36  20175235  阅读(127)  评论(0编辑  收藏  举报