1、四 川 大 学 计 算 机 学 院、软 件 学 院实 验 报 告 学号:_1043111034_ 姓名:陈枝 专业:软件工程 班级:3 第 12 周 课程名称 操作系统 实验课时 7实验项目 多路 I/O 模型 实验时间 2实验目的 并发服务器 III多路 I/O 模型实验环境 Linux 虚拟机 实验内容(算法、程序、步骤和方法) 实验要求:结合教师给的两个参考程序(基于 select 的远程并发服务器和基于 select 的聊天程序),利用 select 函数实现在 Linux 环境下实现一个聊天室程序,要求: 1. 用户默认处于广播模式,一个客户在其客户端发送的消息,其它客户端用户全部可
2、以收到; 2. 程序支持下列命令 /help:显示帮助信息(思考:信息是放在客户端还是服务器端); /quit:用户退出聊天室,同时将退出信息广播给其他用户; /who:显示在线用户; /send 用户名 消息:向指定用户发送点到点消息 服务器:#include #include #include #include #include #include #include /* port were listening on */#define SERVERPORT 1573/*maximum cnnnect number*/#define BACKLOG 10/*maximum data buff
3、er*/#define BUFSIZE 2048struct client_infoint client_id;struct sockaddr_in client_address;int first;char name20;int ishide;int main()/*master file descriptor list*/fd_set master_fds;/*temple file descriptor list for select()*/fd_set read_fds;/*server address*/struct sockaddr_in server_addr;/*client
4、address*/struct sockaddr_in client_addr;/*maximum file descriptor number*/int max_fd;/*listening socket file descriptor*/int sockfd;/*newly accept()ed socket file descreptor*/int newfd;/*buffer for saving client data*/char data_bufBUFSIZE;char send_bufBUFSIZE;/*number of client data*/int nbytes;/*fo
5、r set socket option*/int opt;opt = SO_REUSEADDR;/*lenth of address*/int addr_len;/*for accept() to use*/int size;size = sizeof(struct sockaddr);/*temple varient*/int tmp_i, tmp_j,j;struct client_info clientinfoBACKLOG;/*clear the master and temple file descriptor*/FD_ZERO(FD_ZERO(memset(memset(/*cre
6、ate socket*/if (-1 = (sockfd = socket(AF_INET, SOCK_STREAM, 0)perror(“create socket() error:“);exit(1);/*set the socket*/if (-1 = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, exit(1);/*bind first config the socket then binding*/memset(server_addr.sin_family = AF_INET;server_addr.sin_port = htons(SER
7、VERPORT);server_addr.sin_addr.s_addr = htonl(INADDR_ANY);bzero(if (-1 = bind(sockfd, (struct sockaddr*)exit(1);/*listen */if (-1 = listen(sockfd, BACKLOG)perror(“listen() error:“);exit(1);/*add sockfd to master set*/FD_SET(sockfd, /*keep track the max file descriptor*/max_fd = sockfd;printf(“server
8、is ok!n“);/*loop*/for (j = 0; j max_fd)max_fd = newfd;strcpy(send_buf,“your name is: “);/sprintf(data_buf,“%d“,newfd);/strcat(send_buf,data_buf);write(newfd,send_buf,sizeof(send_buf);send_buf0=0;data_buf0=0;/printf(“Get the new connect from %sn“, inet_ntoa(client_addr.sin_addr);else/*get data from t
9、he client*/nbytes = read(tmp_i, data_buf, sizeof(data_buf);if (0 nbytes)perror(“recv() error:“);exit(1);else if(clientinfotmp_i.first=1)strncpy(clientinfotmp_i.name,data_buf,20);clientinfotmp_i.first=0;else if(nbytes = 0 | (0 = strcmp(data_buf, “exit“)printf(“client: %s exit!n“,clientinfotmp_i.name)
10、;FD_CLR(tmp_i, close(tmp_i);strcat(send_buf, clientinfotmp_i.name);strcat(send_buf, “ was exit!“);clientinfotmp_i.client_id=-1;for (tmp_j = sockfd + 1; tmp_j #include #include #include #include #include #include #include #define CLIENTPORT 1573#define BUFSIZE 2048int main(int argc, char *argv)int so
11、ckfd;fd_set sockset;struct sockaddr_in serveraddr;struct sockaddr_in clientaddr;int recvbytes;char recv_bufBUFSIZE;char send_bufBUFSIZE;int data_len;if (2 argc)printf(“Please input the server ip!n“);exit(1);if (-1 = (sockfd = socket(AF_INET, SOCK_STREAM, 0 )printf(“create the socket error!n“);perror
12、(“create errors are :“);exit(1);memset(serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv1);serveraddr.sin_port = htons(CLIENTPORT);if (-1= connect(sockfd, (struct sockaddr*)exit(1);fprintf(stdout, “client ok!n“);fflush(stdout);while (1)FD_ZERO( FD_SET(sockfd, FD_SET(0, mems
13、et(recv_buf, 0, sizeof(recv_buf);memset(send_buf, 0, sizeof(send_buf);select( sockfd+1, if (FD_ISSET( sockfd, close(sockfd);exit(1);recv_bufrecvbytes = 0;printf(“%sn“, recv_buf);fflush(stdout);if ( FD_ISSET( 0, data_len = strlen(send_buf);send_bufdata_len - 1 = 0;if (send_buf0=/)if (0 = strcmp(send_
14、buf, “/quit“)printf(“quiting from chat room!n“);strcpy(send_buf,“exit“);send_buf4=0;write(sockfd, send_buf, sizeof(send_buf);close(sockfd);exit(1);else if (0 = strcmp(send_buf, “/help“)printf(“/help print help msgn“);printf(“/quit quit chat roomn“);printf(“/who print users who is in chat room nown“)
15、;printf(“/send user msg send msg to the user you point ton“);printf(“/hide hiden“);printf(“/unhide unhiden“);continue;/send the contentif (-1 = write(sockfd, send_buf, sizeof(send_buf)perror(“send data error:“);close(sockfd);exit(1); /quit the chat roomif (0 = strcmp(send_buf, “exit“)printf(“quiting from chat room!n“);close(sockfd);exit(1);/end whileclose(sockfd);return 0;数据记录和计算截图:结 论(结 果) 结果运行正确 ,在服务器端实现了用户的隐身于=与在线功能。小 结 学习了多路 I/0,为以后的进一步学习 linux 打下基础指导老师评 议 成绩评定: 指导教师签名: