您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 电子科技大学网络编程作业(I O复用)4
网络编程作业4作业要求:服务器利用I/O复用技术,实现同时向多个客户提供服务。要求:服务器:接收客户连接请求,并打印客户IP地址及端口号,然后接收客户发来的字符串,并打印该字符串和其来自与哪个客户。同时向客户返回该字符串。当某一客户断开连接时,要求服务器打印该客户输入的所有字符。客户:从命令行接收服务器地址,并向服务器发起连接请求,连接成功后,从标准输入接收字符串并发送给服务器,等待服务器响应并打印接收的信息。程序:服务器端:#includestdlib.h#includestring.h#includestdio.h#includeunistd.h#includepthread.h#includesys/types.h#includesys/socket.h#includenetinet/in.h#includearpa/inet.h#definePORT9999#defineBACKLOG5#defineMAXDATASIZE1000typedefstructCLIENT{intfd;char*name;structsockaddr_inaddr;char*data;}CLIENT;voidprocess_cli(structCLIENT*client,char*recvbuf,intlen);voidsavedata(char*recvbuf,intlen,char*data);intmain(void){inti,maxi,maxfd,sockfd;intnready;ssize_tn;fd_setrset,allset;intlistenfd,connectfd;structsockaddr_inserver;structCLIENTclient[FD_SETSIZE];charrecvbuf[MAXDATASIZE];intsin_size;if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1){perror(Createsocketfailed.);exit(-1);}intopt=SO_REUSEADDR;setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));bzero(&server,sizeof(server));server.sin_family=AF_INET;server.sin_port=htons(PORT);server.sin_addr.s_addr=htonl(INADDR_ANY);if(bind(listenfd,(structsockaddr*)&server,sizeof(structsockaddr))==-1){perror(Binderror.);exit(-1);}if(listen(listenfd,BACKLOG)==-1){perror(listenerror.);exit(-1);}sin_size=sizeof(structsockaddr_in);maxfd=listenfd;maxi=-1;for(i=0;iFD_SETSIZE;i++)client[i].fd=-1;FD_ZERO(&allset);FD_SET(listenfd,&allset);while(1){structsockaddr_inaddr;rset=allset;nready=select(maxfd+1,&rset,NULL,NULL,NULL);if(FD_ISSET(listenfd,&rset)){if((connectfd=accept(listenfd,(structsockaddr*)&addr,&sin_size))==-1){perror(accepterror.);continue;}for(i=0;iFD_SETSIZE;i++){if(client[i].fd0){client[i].fd=connectfd;client[i].name=malloc(sizeof(char)*MAXDATASIZE);client[i].addr=addr;client[i].data=malloc(sizeof(char)*MAXDATASIZE);client[i].name[0]='\0';client[i].data[0]='\0';printf(Yougotaconnectfrom%s:%d.\n,inet_ntoa(client[i].addr.sin_addr),client[i].addr.sin_port);send(connectfd,Welcome\n,22,0);break;}}if(i==FD_SETSIZE)printf(toomanycllients.\n);FD_SET(connectfd,&allset);if(connectfdmaxfd)maxfd=connectfd;if(imaxi)maxi=i;if(--nready=0)continue;}for(i=0;i=maxi;i++){if((sockfd=client[i].fd)0)continue;if(FD_ISSET(sockfd,&rset)){if((n=recv(sockfd,recvbuf,MAXDATASIZE,0))==0){close(sockfd);printf(Client(%s)closedconnection.User’sdata:%s\n,client[i].name,client[i].data);FD_CLR(sockfd,&allset);client[i].fd=-1;free(client[i].name);free(client[i].data);}elseprocess_cli(&client[i],recvbuf,n);if(--nready=0)break;}}}close(listenfd);}voidprocess_cli(CLIENT*client,char*recvbuf,intlen){inti1;charsendbuf[MAXDATASIZE];recvbuf[len]='\0';if(strlen(client-name)==0){memcpy(client-name,recvbuf,len);printf(Client'snameis%s.\n,client-name);return;}printf(Receivedclient(%s)message:%s\n,client-name,recvbuf);savedata(recvbuf,len,client-data);for(i1=0;i1len-1;i1++){sendbuf[i1]=recvbuf[len-i1-2];}sendbuf[len-1]='\0';send(client-fd,sendbuf,strlen(sendbuf),0);}voidsavedata(char*recvbuf,intlen,char*data){intstart=strlen(data);inti;for(i=0;ilen;i++){data[start+i]=recvbuf[i];}}客户端:#includeunistd.h#includestdio.h#includestring.h#includestdlib.h#includesys/socket.h#includenetinet/in.h#includenetdb.h#definePORT9999#defineMAXDATASIZE100intmain(intargc,char*argv[]){intfd,numbytes;charbuf[MAXDATASIZE];structhostent*he;structsockaddr_inserver;inti=1;if(argc!=2){printf(Usage:%sIPaddress\n,argv[0]);exit(-1);}if((he=gethostbyname(argv[1]))==NULL){perror(gethostbynameerror.);exit(-1);}if((fd=socket(AF_INET,SOCK_STREAM,0))==-1){perror(Createsocketfailed.);exit(1);}bzero(&server,sizeof(server));server.sin_family=AF_INET;server.sin_port=htons(PORT);server.sin_addr=*((structin_addr*)he-h_addr);if(i=connect(fd,(structsockaddr*)&server,sizeof(structsockaddr))==-1){perror(connectfailed.);exit(1);}if(((numbytes=recv(fd,buf,MAXDATASIZE,0))==-1)){perror(recverror.);exit(1);}buf[numbytes]='\0';printf(ServerMessage:%s,buf);printf(Pleaseinputyourname:);scanf(%s,buf);if(((numbytes=send(fd,buf,strlen(buf),0))==-1)){perror(senderror.);exit(1);}while(1){printf(Pleaseinputmessage(maxchar:%d):,MAXDATASIZE);scanf(%s,buf);if(strlen(buf)1)i=0;if(((numbytes=send(fd,buf,strlen(buf),0))==-1)){perror(senderror.);exit(1);}if(((numbytes=recv(fd,buf,MAXDATASIZE,0))==-1)){perror(readerror.);exit(1);}buf[numbytes]='\0';}close(fd);}实验结果:服务器:客户端1:客户端2:
本文标题:电子科技大学网络编程作业(I O复用)4
链接地址:https://www.777doc.com/doc-4995695 .html