?* ? 描 ? ?述:select多路复用实现并发,回传客户端数据 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <strings.h> #include <sys/types.h>? #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <sys/select.h> #include <sys/time.h>
#define SIZE 128
int init_server(char *ipaddr,unsigned short port,int backflog);//服务器初始化函数 int main() { ?? ?int sockfd=init_server("192.168.9.253",7777,1024);//服务器IP地址 端口号
?? ?fd_set rfds,fds; ?? ?FD_ZERO(&rfds); ?? ?FD_SET(0,&rfds); ?? ?FD_SET(sockfd,&rfds); ?? ?int nfds=sockfd+1;
?? ?char buf[SIZE]={0}; ?? ?int ret=0,i=0; ?? ?while(1) ?? ?{ ?? ??? ?fds=rfds; ?? ??? ?select(nfds,&fds,NULL,NULL,NULL);
?? ??? ?for(i=0;i<nfds;i++) ?? ??? ?{ ?? ??? ??? ?if(FD_ISSET(i,&fds)) ?? ??? ??? ?{ ?? ??? ??? ??? ?if(i==0) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?bzero(&buf,sizeof(buf)); ?? ??? ??? ??? ??? ?fgets(buf,sizeof(buf)-1,stdin); ?? ??? ??? ??? ??? ?buf[strlen(buf)-1]='\0'; ?? ??? ??? ??? ?}else if(i==sockfd) ?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?struct sockaddr_in caddr; ?? ??? ??? ??? ??? ?bzero(&caddr,sizeof(caddr)); ?? ??? ??? ??? ??? ?socklen_t clen=sizeof(caddr); ?? ??? ??? ??? ??? ?int rws=accept(sockfd,(struct sockaddr*)&caddr,&clen); ?? ??? ??? ??? ??? ?if(-1==rws) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?perror("accept"); ?? ??? ??? ??? ??? ??? ?close(i); ?? ??? ??? ??? ??? ??? ?return -1; ?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?printf("%s:%u\n",inet_ntoa(caddr.sin_addr),ntohs(caddr.sin_port)); ?? ??? ??? ??? ??? ?FD_SET(rws,&rfds); ?? ??? ??? ??? ??? ?nfds=nfds>rws+1? nfds:rws+1; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?bzero(&buf,sizeof(buf)); ?? ??? ??? ??? ??? ?ret=read(i,buf,sizeof(buf)); ?? ??? ??? ??? ??? ?if(-1==ret) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?perror("read"); ?? ??? ??? ??? ??? ??? ?FD_CLR(i,&rfds); ?? ??? ??? ??? ??? ??? ?close(i); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?else if(0==ret) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?printf("client close"); ?? ??? ??? ??? ??? ??? ?FD_CLR(i,&rfds); ?? ??? ??? ??? ??? ??? ?close(i); ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?printf("buf:%s",buf); ?? ??? ??? ??? ??? ?ret=write(i,buf,sizeof(buf)); ?? ??? ??? ??? ??? ?if(-1==ret) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?perror("write"); ?? ??? ??? ??? ??? ??? ?FD_CLR(i,&rfds); ?? ??? ??? ??? ??? ??? ?close(i); ?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?}
?? ?close(sockfd); ?? ?return 0;
} int init_server(char *ipaddr,unsigned short port,int backflog) { ?? ?int sockfd=socket(AF_INET,SOCK_STREAM,0); ?? ?if(sockfd==-1) ?? ?{ ?? ??? ?perror("socket"); ?? ??? ?return -1; ?? ?} ?? ?printf("sockfd=%d\n",sockfd);
?? ?struct sockaddr_in saddr;// ?? ?bzero(&saddr,sizeof(saddr)); ?? ?saddr.sin_family=AF_INET; ?? ?saddr.sin_port=htons(port); ?? ?saddr.sin_addr.s_addr=inet_addr(ipaddr);
?? ?socklen_t len=sizeof(saddr);
?? ?int ret =bind(sockfd,(struct sockaddr*)&saddr,len); ?? ?if(ret==-1) ?? ?{ ?? ??? ?perror("bind"); ?? ??? ?return -1; ?? ?} ?? ?if(-1==listen(sockfd,backflog)) ?? ?{ ?? ??? ?perror("listen"); ?? ??? ?return -1; ?? ?}
?? ?return sockfd; }
?
|