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*512int 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?\n";if((portnumber=atoi("155207"))<0){fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);exit(1);}/* 服務器端開始建立socket描述符 */if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) {fprintf(stderr,"Socket error:%s\n\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\n\a",strerror(errno));exit(1);}/* 監聽sockfd描述符 */if(listen(sockfd,5)==-1) {fprintf(stderr,"Listen error:%s\n\a",strerror(errno));exit(1);}while(1){fprintf(stderr, "server is listening!\n");/* 服務器阻塞,直到客戶程序建立連接 */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\n\a",strerror(errno));exit(1);}fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));if(write(new_fd,hello,strlen(hello))==-1) {fprintf(stderr,"Write Error:%s\n",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*512int 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?\n";if((portnumber=atoi("155207"))<0){fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);exit(1);}/* 服務器端開始建立socket描述符 */if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) {fprintf(stderr,"Socket error:%s\n\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\n\a",strerror(errno));exit(1);}/* 監聽sockfd描述符 */if(listen(sockfd,5)==-1) {fprintf(stderr,"Listen error:%s\n\a",strerror(errno));exit(1);}while(1){fprintf(stderr, "server is listening!\n");/* 服務器阻塞,直到客戶程序建立連接 */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\n\a",strerror(errno));exit(1);}fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));if(write(new_fd,hello,strlen(hello))==-1) {fprintf(stderr,"Write Error:%s\n",strerror(errno));
轉載于:https://www.cnblogs.com/lnaswxc/p/7859330.html
總結
以上是生活随笔為你收集整理的2017-2018-1 20155223 实验三 实时系统的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。