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

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

学习使用Linux命令wc(1)

基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端

客户端传一个文本文件给服务器

服务器返加文本文件中的单词数

  • 使用man 1 wc查看wc
  • 代码实现如下:
  • 客户端
#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]="服务器实现者学号:20155209”;

    char str3[MAXLINE]="当地时间:";

    FILE *fp;

    char filename[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){

        printf("输入文件名(.txt):\n");

        gets(filename);

        fp = fopen(filename,"rb");

        while(!feof(fp)){

        fgets(sbuf,MAXLINE,fp);

        send(clientfd,sbuf,MAXLINE,0);

        bzero(sbuf,sizeof(sbuf));
        }

        printf("%s",str1);
        puts(host);

        printf("%s",str2);
        putchar(‘\n‘);

        //printf("%s",str3);

        //puts(rbuf);

        fclose(fp);

        //sleep(1000);

        //recv(clientfd,rbuf,MAXLINE,0);

        //printf("传输完成,该文件单词数为%s\n!",rbuf);

        //puts(rbuf);

        close(clientfd);

        exit(0);
    }

}

  • 服务器

#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;
}

void itoa (int n,char s[])
{
    int i,j,sign;
    char temp[MAXLINE];

    if((sign=n)<0)//记录符号
        n=-n;//使n成为正数
    i=0;
    do{
               temp[i++]=n%10+‘0‘;//取下一个数字
    }
    while ((n/=10)>0);//删除该数字
    if(sign<0)
        temp[i++]=‘-‘;

    for(j=0;j<i;j++){//生成的数字是逆序的,所以要逆序输出
        s[j] = temp[i-j-1];
    }
    s[i] = ‘\0‘;
} 

int main(int argc,char **argv){

    int count=0,cnt=0,listenfd,connfd,port,clientlen;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char *haddrp;
    char sbuf[MAXLINE];
   // char *bufp = sbuf;
    char rbuf[MAXLINE];
    rio_t rio;
    time_t lt;
    tm *local;
    char str1[MAXLINE]="客户端IP:";
    char str2[MAXLINE]="服务器实现者学号:";
    char str3[MAXLINE]="当地时间:";

    FILE *fp = fopen("test.txt","wb");

    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);
        */

        while(cnt = recv(connfd,rbuf,MAXLINE,0)){

        //printf("%d\n",recv(connfd,rbuf,MAXLINE,0));
        fputs(rbuf,fp);
        bzero(rbuf,sizeof(rbuf));

        count += cnt;
        }

        //printf("传输成功!,该文件单词数共%d\n!",count);

        fclose(fp);

        itoa(count,sbuf);

        //send(connfd,sbuf,MAXLINE,0);

        printf("该文件单词数为%s!\n",sbuf);

        close(connfd);
        exit(0);
        }

        close(connfd);
    }
}
  • 实现截图:

使用多线程实现wc服务器并使用同步互斥机制保证计数正确

上方提交代码

下方提交测试

对比单线程版本的性能,并分析原因

  • 代码实现如下:
  • 客户端

#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]="服务器实现者学号:20155209”;

    char str3[MAXLINE]="当地时间:";

    FILE *fp;

    char filename[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){

        printf("输入文件名(.txt):\n");

        gets(filename);

        fp = fopen(filename,"rb");

        while(!feof(fp)){

        fgets(sbuf,MAXLINE,fp);

        send(clientfd,sbuf,MAXLINE,0);

        bzero(sbuf,sizeof(sbuf));
        }

        printf("%s",str1);
        puts(host);

        printf("%s",str2);
        putchar(‘\n‘);

        //printf("%s",str3);

        //puts(rbuf);

        fclose(fp);

        //sleep(1000);

        //recv(clientfd,rbuf,MAXLINE,0);

        //printf("传输完成,该文件单词数为%s\n!",rbuf);

        //puts(rbuf);

        close(clientfd);

        exit(0);
    }

}
  • 服务器
#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>
#include <pthread.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 itoa (int n,char s[]);

void *thread(void *vargp);

int main(int argc,char **argv){

    int listenfd,*connfdp,port;
    int clientlen;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char *haddrp;
    pthread_t tid;

    if(argc != 2){

        fprintf(stderr,"usage:%s <port>\n",argv[0]);
        exit(0);
    }

    port = atoi(argv[1]);

    listenfd = open_listenfd(port);

    while(1){

        clientlen = sizeof(clientaddr);

        connfdp =malloc(sizeof(int));

        *connfdp = 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);

        pthread_create(&tid,NULL,thread,connfdp);

        pthread_join(tid,NULL);
    }
}

void *thread(void *vargp){

    time_t lt;
    tm *local;
    char sbuf[MAXLINE];
    int cnt = 0,count = 0;
    char *fp = fopen("test.txt","wb");
    char rbuf[MAXLINE];
    int connfd = *((int*)vargp);

    free(vargp);

    pthread_detach(pthread_self());

    /*lt = time(NULL);
    local = localtime(&lt);
    strftime(sbuf,64,"%Y-%m-%d %H:%M:%S",local);
    send(connfd,sbuf,MAXLINE,0);
    */
while(cnt = recv(connfd,rbuf,MAXLINE,0)){

        //printf("%d\n",recv(connfd,rbuf,MAXLINE,0));
        fputs(rbuf,fp);
        bzero(rbuf,sizeof(rbuf));

        count += cnt;
        }

        //printf("传输成功!,该文件单词数共%d\n!",count);

        fclose(fp);

        itoa(count,sbuf);

        //send(connfd,sbuf,MAXLINE,0);

        printf("该文件单词数为%s!\n",sbuf);

    close(connfd);

    return NULL;
}

void itoa (int n,char s[])
{
    int i,j,sign;
    char temp[MAXLINE];

    if((sign=n)<0)//记录符号
        n=-n;//使n成为正数
    i=0;
    do{
               temp[i++]=n%10+‘0‘;//取下一个数字
    }
    while ((n/=10)>0);//删除该数字
    if(sign<0)
        temp[i++]=‘-‘;

    for(j=0;j<i;j++){//生成的数字是逆序的,所以要逆序输出
        s[j] = temp[i-j-1];
    }
    s[i] = ‘\0‘;
}
  • 程序运行结果截图:
  • 实验中遇到的问题:在自己电脑(Mac)运行代码出现如下问题:Static library link issue with Mac OS X: symbol(s) not found for architecture x86_64。
  • 解决方法:在网上寻找了许多方法都不能编译成功。最后在Mac里安装了Linux虚拟机,在虚拟机里做这个实验了。
时间: 2024-07-31 04:44:39

2017-2018-1 20155209 实验三 实时系统的相关文章

20145216 20145330《信息安全系统设计基础》实验三 实时系统的移植

20145216 20145330<信息安全系统设计基础>实验三 实时系统的移植 实验报告封面 实验内容 连接实验箱电源,用串口线.并口线.网线.连接实验箱和主机 安装ADS 安装GIVEIO驱动 安装JTAG驱动 配置超级终端 测试基本安装是否正确 实验步骤 连接实验箱电源,用串口线.并口线.网线.连接实验箱和主机 安装ADS 在00-ads1.2目录下找到安装文件,一路默认安装即可 在00-ads1.2\Crack目录下找到破解文件,进行破解,破解方法如下: 点击开始>所有程序>

20145311 《信息安全系统设计基础》实验三 实时系统的移植

20145311 <信息安全系统设计基础>实验三 实时系统的移植 北京电子科技学院(BESTI) 实验报告 课程:信息安全系统设计基础 班级:1453姓名:王亦徐 黄志远学号:20145311 20145211成绩: 指导教师:娄嘉鹏 实验日期:2016.11.17实验密级: 预习程度: 实验时间:10:10-12:25仪器组次:11 必修/选修: 必修 实验序号:三实验名称:实时系统的移植实验目的与要求:1.按照要求正确实验箱电源,用串口线.并口线.网线.连接实验箱和主机.2.正确安装软件和

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

2017-2018-1 20155229 实验三 实时系统 实验目的 了解实时系统的信息.特点等内容. 学习客户端和服务器之间的工作原理,并编写代码实现. 实验步骤 实验三-并发程序-1 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 wc命令的功能: 统计指定文件中的字节数.字数.行数,并将统计结果显示输出.该命令统计指定文件中的字节数.字数.行数.如果没有

2017-2018-1 20155208 20155212 实验三 实时系统

2017-2018-1 20155212 实验三 实时系统 1 学习使用Linux命令wc(1) 题目 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 步骤 使用man wc命令查看wc wc命令详解 语法:wc [选项] 文件 选项含义 c:统计字节数 l:统计行数 w:统计字数 使用示例 实现难点: 如何统计单词数? 使用od -tc命令查看文本中单词之间如何间隔 单词间通过' '.'\r

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

2017-2018-1 20155311 实验三 实时系统 实验内容 任务一 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位155203)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 实现mywc Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 命令格式:wc[选项]文件... 命令功能:统计指定文件中的字节数.字数.行数,并将统计结果

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

2017-2018-1 20155305实验三 实时系统 任务一 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 上方提交代码 附件提交测试截图,至少要测试附件中的两个文件 使用man wc命令查看wc命令的基本用法: 可知wc命令的功能为:统计指定文件中的字节数.字数.行数等,并将统计结果显示输出.常用的参数为: -c:统计字节数 -l:统计行数 -m:统计

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

2017-2018-1 20155227 实验三 实时系统 实验目的,实验步骤 实验过程如下. 实验三-并发程序-1 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 上方提交代码 附件提交测试截图,至少要测试附件中的两个文件 client.c: #include<netinet/in.h> // sockaddr_in #include<sys/typ

2017-2018-1 20155234 实验三 实时系统及mypwd实现

2017-2018-1 20155234实验三实时系统及mypwd实现 实验三-并发程序-1 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 使用man命令查看wc的用法 自己实现的wc截图: 实验三-并发程序-2 使用多线程实现wc服务器并使用同步互斥机制保证计数正确 对比单线程版本的性能,并分析原因 截图: mypwd实现 使用man命令查看wc的用法 m

2017-2018-1 20155235 实验三 实时系统 实验内容

2017-2018-1 20155235 实验三 实时系统 实验内容 一.并发程序-1 二.并发程序-2 三.并发程序-3 实验步骤 一.并发程序-1 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 wc命令的学习 Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项