2019-2020-1《信息安全系统设计》20175311 20175324 20175330 实验三实时系统
作者:互联网
2019-2020-1《信息安全系统设计》20175311 20175324 20175330 实验三实时系统
任务一
- 学习使用Linux命令wc(1)
- 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端
- 客户端传一个文本文件给服务器
- 服务器返加文本文件中的单词数
使用多线程实现wc服务器并使用同步互斥机制保证计数正确
实验操作
任务一 :- 实现wc功能实现
- 客户端给服务器传文件功能
- 在客户端调用wc函数统计传过来的文件的单词个数
wc命令参数:-
用wc命令做到只打印统计数字不打印文件名:cat test.txt |wc -l
- -c 统计字节数。
- -l 统计行数。
- -m 统计字符数。这个标志不能与 -c 标志一起使用。
- -w 统计字数。一个字被定义为由空白、跳格或换行字符分隔的字符串。
- -L 打印最长行的长度。
- -help 显示帮助信息
- --version 显示版本信息
服务器代码:
#include <netinet/in.h> // for sockaddr_in
#include <sys/types.h> // for socket
#include <sys/socket.h> // for socket
#include <stdio.h> // for printf
#include <stdlib.h> // for exit
#include <string.h> // for bzero
#define HELLO_WORLD_SERVER_PORT 6666
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
int main(int argc, char **argv)
{
//设置一个socket地址结构server_addr,代表服务器internet地址, 端口
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr)); //把一段内存区的内容全部设置为0
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(155202);
server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);
//创建用于internet的流协议(TCP)socket,用server_socket代表服务器socket
int server_socket = socket(PF_INET,SOCK_STREAM,0);
if( server_socket < 0)
{
printf("Create Socket Failed!");
exit(1);
}
//把socket和socket地址结构联系起来
if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)))
{
printf("Server Bind Port : %d Failed!", HELLO_WORLD_SERVER_PORT);
exit(1);
}
//server_socket用于监听
if ( listen(server_socket, LENGTH_OF_LISTEN_QUEUE) )
{
printf("Server Listen Failed!");
exit(1);
}
while (1) //服务器端要一直运行
{
//定义客户端的socket地址结构client_addr
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
//接受一个到server_socket代表的socket的一个连接
//如果没有连接请求,就等待到有连接请求--这是accept函数的特性
//accept函数返回一个新的socket,这个socket(new_server_socket)用于同连接到的客户的通信
//new_server_socket代表了服务器和客户端之间的一个通信通道
//accept函数把连接到的客户端信息填写到客户端的socket地址结构client_addr中
int new_server_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length);
if ( new_server_socket < 0)
{
printf("Server Accept Failed!\n");
break;
}
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
length = recv(new_server_socket,buffer,BUFFER_SIZE,0);
if (length < 0)
{
printf("Server Recieve Data Failed!\n");
break;
}
char file_name[FILE_NAME_MAX_SIZE+1];
bzero(file_name, FILE_NAME_MAX_SIZE+1);
strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:strlen(buffer));
// int fp = open(file_name, O_RDONLY);// if( fp < 0 )
FILE * fp = fopen(file_name,"r");
if(NULL == fp )
{
printf("File:\t%s Not Found\n", file_name);
}
else
{
bzero(buffer, BUFFER_SIZE);
int file_block_length = 0;
// while( (file_block_length = read(fp,buffer,BUFFER_SIZE))>0)
while( (file_block_length = fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0)
{
printf("file_block_length = %d\n",file_block_length);
//发送buffer中的字符串到new_server_socket,实际是给客户端
if(send(new_server_socket,buffer,file_block_length,0)<0)
{
printf("Send File:\t%s Failed\n", file_name);
break;
}
bzero(buffer, BUFFER_SIZE);
}
// close(fp);
fclose(fp);
printf("File:\t%s Transfer Finished\n",file_name);
}
//关闭与客户端的连接
close(new_server_socket);
}
//关闭监听用的socket
close(server_socket);
return 0;
}
客户端代码:
#include <netinet/in.h> // for sockaddr_in
#include <sys/types.h> // for socket
#include <sys/socket.h> // for socket
#include <stdio.h> // for printf
#include <stdlib.h> // for exit
#include <string.h> // for bzero
#define HELLO_WORLD_SERVER_PORT 6666 #define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
#define INADDR_ANY 175311
int main(int argc, char **argv){
if (argc != 2)
{
printf("Usage: ./%s ServerIPAddress\n",argv[0]);
exit(1);
}
//设置一个socket地址结构client_addr,代表客户机internet地址, 端口
struct sockaddr_in client_addr;
bzero(&client_addr,sizeof(client_addr)); //把一段内存区的内容全部设置为0
client_addr.sin_family = AF_INET; //internet协议族
client_addr.sin_addr.s_addr = htons(INADDR_ANY);//INADDR_ANY表示自动获取本机地址
client_addr.sin_port = htons(0); //0表示让系统自动分配一个空闲端口
//创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket
int client_socket = socket(AF_INET,SOCK_STREAM,0);
if( client_socket < 0)
{
printf("Create Socket Failed!\n");
exit(1);
}
//把客户机的socket和客户机的socket地址结构联系起来
if( bind(client_socket,(struct sockaddr*)&client_addr,sizeof(client_addr)))
{
printf("Client Bind Port Failed!\n");
exit(1);
}
//设置一个socket地址结构server_addr,代表服务器的internet地址, 端口
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
if(inet_aton(argv[1],&server_addr.sin_addr) == 0) //服务器的IP地址来自程序的参数
{
printf("Server IP Address Error!\n");
exit(1);
}
server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);
socklen_t server_addr_length = sizeof(server_addr);
//向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
if(connect(client_socket,(struct sockaddr*)&server_addr, server_addr_length) < 0)
{
printf("Can Not Connect To %s!\n",argv[1]);
exit(1);
}
char file_name[FILE_NAME_MAX_SIZE+1];
bzero(file_name, FILE_NAME_MAX_SIZE+1);
printf("Please Input File Name On Server:\t");
scanf("%s", file_name);
char buffer[BUFFER_SIZE];
bzero(buffer,BUFFER_SIZE);
strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE?BUFFER_SIZE:strlen(file_name));
//向服务器发送buffer中的数据
send(client_socket,buffer,BUFFER_SIZE,0);
// int fp = open(file_name, O_WRONLY|O_CREAT);// if( fp < 0 )
FILE * fp = fopen(file_name,"w");
if(NULL == fp )
{
printf("File:\t%s Can Not Open To Write\n", file_name);
exit(1);
}
//从服务器接收数据到buffer中
bzero(buffer,BUFFER_SIZE);
int length = 0;
while( length = recv(client_socket,buffer,BUFFER_SIZE,0))
{
if(length < 0)
{
printf("Recieve Data From Server %s Failed!\n", argv[1]);
break;
}
// int write_length = write(fp, buffer,length);
int write_length = fwrite(buffer,sizeof(char),length,fp);
if (write_length<length)
{
printf("File:\t%s Write Failed\n", file_name);
break;
}
bzero(buffer,BUFFER_SIZE);
}
printf("Recieve File:\t %s From Server[%s] Finished\n",file_name, argv[1]);
fclose(fp);
//关闭socket
close(client_socket);
return 0;
}
实验三-并发程序-2
- 使用多线程实现wc服务器并使用同步互斥机制保证计数正确
- 对比单线程版本的性能,并分析原因
- 多线程实现wc服务器时,会出现多个客户端同时像服务器传送文件的情况,所以仅仅在服务器端创建一个recvfile.txt用于接收文件是远远不够的,需要根据发送的不同文件名创建新的接收文件。
实验代码
服务器:
#include"csapp.h"
#include<stdio.h>
#include<stdlib.h>
static int byte_cnt;
static sem_t mutex;
#define NTHREADS 4
#define SBUFSIZE 16
typedef struct {
int *buf; /* Buffer array
int n; /* Maximum number of slots
int front; /* buf[(front+1)%n] is first item
int rear; /* buf[rear%n] is last item
sem_t mutex; /* Protects accesses to buf
sem_t slots; /* Counts available slots
sem_t items; /* Counts available items
} sbuf_t;
void echo_cnt(int connfd);
void *thread(void *vargp);
int wc(char *name){
char ch;
FILE *fp;
long count=0;
char s[21];
if ((fp=fopen(name,"r+"))==NULL)
{
fprintf(stderr,"不能打开文件\n");
exit(EXIT_FAILURE);
}
while(fscanf(fp,"%s",s)!=EOF)
count++;
fclose(fp);
printf("File %s has %ld characters\n",name,count);
return 0;
}
sbuf_t sbuf; /* shared buffer of connected descriptors */
int main(int argc, char **argv){
int i, listenfd, connfd, port, clientlen=sizeof(struct sockaddr_in);
struct sockaddr_in clientaddr;
pthread_t tid;
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(0);
}
port = atoi(argv[1]);
sbuf_init(&sbuf, SBUFSIZE);
listenfd = Open_listenfd(port);
for (i = 0; i < NTHREADS; i++) /* Create worker threads */
Pthread_create(&tid, NULL, thread, NULL);
while (1) {
connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen);
sbuf_insert(&sbuf, connfd); /* Insert connfd in buffer */
}
}
static void init_echo_cnt(void){
Sem_init(&mutex, 0, 1);
byte_cnt = 0;
}
void echo_cnt(int connfd){
int n,x;
long int count;
char buf[MAXLINE];
char name[MAXLINE]
rio_t rio;
static pthread_once_t once = PTHREAD_ONCE_INIT;
Pthread_once(&once, init_echo_cnt);
Rio_readinitb(&rio, connfd);
while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) {
P(&mutex);
byte_cnt += n;
/*x = sizeof(buf);
buf[x] = 0;
count = wc(buf);*/
printf("thread %d received %d (%d total) bytes on fd %d\n",
(int) pthread_self(), n, byte_cnt, connfd);
//name = buf;
V(&mutex);
//sprint(buf,"%s:%ld characters".count);
Rio_writen(connfd, buf, n);
}
}
void sbuf_init(sbuf_t *sp, int n){
sp->buf = Calloc(n, sizeof(int));
sp->n = n; /* Buffer holds max of n items */
sp->front = sp->rear = 0; /* Empty buffer iff front == rear */
Sem_init(&sp->mutex, 0, 1); /* Binary semaphore for locking */
Sem_init(&sp->slots, 0, n); /* Initially, buf has n empty slots */
Sem_init(&sp->items, 0, 0); /* Initially, buf has zero data items */
}
/* $end sbuf_init */
/* Clean up buffer sp *//* $begin sbuf_deinit */void sbuf_deinit(sbuf_t *sp){
Free(sp->buf);
}
/* $end sbuf_deinit */
/* Insert item onto the rear of shared buffer sp *//* $begin sbuf_insert */void sbuf_insert(sbuf_t *sp, int item){
P(&sp->slots); /* Wait for available slot */
P(&sp->mutex); /* Lock the buffer */
sp->buf[(++sp->rear)%(sp->n)] = item; /* Insert the item */
V(&sp->mutex); /* Unlock the buffer */
V(&sp->items); /* Announce available item */
}
/* $end sbuf_insert */
/* Remove and return the first item from buffer sp *//* $begin sbuf_remove */int sbuf_remove(sbuf_t *sp){
int item;
P(&sp->items); /* Wait for available item */
P(&sp->mutex); /* Lock the buffer */
item = sp->buf[(++sp->front)%(sp->n)]; /* Remove the item */
V(&sp->mutex); /* Unlock the buffer */
V(&sp->slots); /* Announce available slot */
return item;
}
void *thread(void *vargp){
Pthread_detach(pthread_self());
while (1) {
int connfd = sbuf_remove(&sbuf); /* Remove connfd from buffer */
echo_cnt(connfd); /* Service client */
Close(connfd);
}
}
客户端:
#include"csapp.h"
#include<stdio.h>
#include<stdlib.h>
int wc(char *name){
char ch;
FILE *fp;
long count=0;
char s[21];
if ((fp=fopen("test1.txt","r+"))==NULL)
{
fprintf(stderr,"不能打开文件%s\n",name);
exit(EXIT_FAILURE);
}
while(fscanf(fp,"%s",s)!=EOF)
count++;
fclose(fp);
printf("File %s has %ld characters\n",name,count);
return 0;
}
int main(int argc, char **argv){
int clientfd, port,n,count;
char *host, buf[MAXLINE];
rio_t rio;
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);
Rio_readinitb(&rio, clientfd);
while (Fgets(buf, MAXLINE, stdin) != NULL) {
if((num=recv(sockfd,buf,MAXDATASIZE,0))==-1)
{
printf("recv() error\n");
exit(1);
}
buf[num-1]='\0';
Rio_writen(clientfd, buf, strlen(buf));
Rio_readlineb(&rio, buf, MAXLINE);
Fputs(buf, stdout);
}
Close(clientfd);
exit(0);
}
/* $end echoclientmain *//* $begin echoclientmain *///#include "csapp.h"/*int main(int argc, char **argv)
{
int clientfd, port;
char *host, buf[MAXLINE];
char *name;
rio_t rio;
FILE *fp;
if (argc != 4) {
fprintf(stderr, "usage: %s <host> <port> <filename>\n", argv[0]);
exit(0);
}
host = argv[1];
port = atoi(argv[2]);
name = argv[3];
clientfd = Open_clientfd(host, port);
Rio_readinitb(&rio, clientfd);
fp=fopen(name,"r+");
while (Fgets(buf, MAXLINE,fp) != NULL) {
Rio_writen(clientfd, buf, strlen(buf));
Rio_readlineb(&rio, buf, MAXLINE);
Fputs(buf, stdout);
}
Close(clientfd);
exit(0);
}
标签:gt,20175324,socket,quot,int,server,20175330,2020,addr 来源: https://www.cnblogs.com/hujidong42/p/11922163.html