How to implement select asynchronous communication under Linux

07-20-2023

The knowledge points of this article on how to implement select asynchronous communication under Linux are not well understood by most people, so the editor summarizes the following content for you. The content is detailed, the steps are clear, and it has certain reference value.

1. Server

/*select_server.c 2011.9.2 by yyg*/ #include #include #include #include #include #include #include #include #include #include #include #include #include #define MAXBUF 1024 int main(int argc, char **argv){ int sockfd,new_fd; socklen_t len; struct sockaddr_in my_addr, their_addr; unsigned int myport,lisnum; char buf[MAXBUF+1]; fd_set rfds; struct timeval(argv[1]){ myport = atoi(argv[1]); } else myport = 7838; if(argv[2]){ lisnum = atoi(argv[2]); } else lisnum =2; if((sockfd = socket(PF_INET,SOCK_STREAM,0))== -1){ perror("socket"); exit(1); } bzero(&my_addr,sizeof(my_addr)); my_addr.sin_family = PF_INET; my_addr.sin_port = htons(myport); if(argv[3]) my_addr.sin_addr.s_addr = INADDR_ANY; the if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1){ perror("bind"); exit(1); } the if(listen(sockfd, lisnum) == -1){ perror("listen"); exit(1); } the while(1){ printf("\n----waiting for new connecting to start new char----\n"); len = sizeof(struct sockaddr); if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&len)) == -1){ perror("accept"); exit(errno); } else printf("server: got connection from %s,port %d,socked %d\n",\   inet_ntoa(their_addr.sin_addr),\ ntohs(their_addr. sin_port), new_fd);   /*Start processing data sending and receiving on each new connection*/ //printf("\n---ready to go.now you can chatting...input enter,then you can chat---\n"); while(1){ /*Empty the collection*/ FD_ZERO(&rfds); /*Add standard input handle 0 to the collection*/ FD_SET(0,&rfds); maxfd = 0; /*Add the current connection handle new_fd to the collection*/ FD_SET(new_fd,&rfds); if(new_fd > maxfd) maxfd = new_fd; /*Set the maximum waiting time*/ tv.tv_sec = 1; tv.tv_usec = 0; retval = select(maxfd+1,&rfds,NULL,NULL,&tv); if(retval == -1){ printf("select error!%s\n", strerror(errno)); break; } `` else if(retval == 0){ //printf("no message come, user no press the button, please wait...\n"); continue; } else{ If(FD_ISSET(new_fd,&rfds)){ /* If there is a message on the connected socket, it will be received and displayed */       bzero(buf,MAXBUF+1); /*Receive the message sent by the other party, up to MAXBUF bytes*/ len = recv(new_fd, buf, MAXBUF, 0); if(len > 0)       printf("recv msg success:%s! %d bytes rcv.\n ",buf,len); else{         if(len < 0){       printf("recv msg fail.the errno is:%d,error info is %s.\n",errno,strerror(errno)); } else printf("quit.\n"); break; } }// FD_ISSET = sockfd case If(FD_ISSET(0,&rfds)){/*If the user has input, then read the content and send it*/ bzero(buf, MAXBUF+1); fgets(buf, MAXBUF, stdin); If(!strncasecmp(buf, "quit", 4)){ printf("self request to quit the chating\n"); break; } /*Send a message to the server*/ len = send(new_fd, buf, strlen(buf)-1 , 0); `` if(len < 0){ printf("mgs:%s send fail!errno is:%d,error info is:%s\n", buf, errno, strerror(errno)); break; }else{      printf("msg: %s\t send success, send %d bytes!\n", buf, len); } }//FD_ISSET = 0 `` }//select processing ends `` }/*inside while*/ close(new_fd); /* Handle the end of data sending and receiving on each new connection */ printf("would you want to chatting with another one?(no->quit)"); fflush(stdout); bzero(buf,MAXBUF+1); fgets(buf,MAXBUF,stdin); if(!strncasecmp(buf,"no",2)){ printf("quit the chatting!\n"); break; } the }/*outer while*/ close(sockfd); return 0; }

2. Client

/*select_client.c 2011.9.2 by yyg*/ #include #include #include #include #include #include #include #include #include #include #include #include #include #define MAXBUF 1024 int main(int argc, char **argv){ int sockfd,len; struct sockaddr_in dest; char buffer[MAXBUF+1]; fd_set rfds; struct timeval(argc != 3){ printf("the param style wrong!\n"); exit(0); } /*Create a socket for tcp communication*/ if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0){ perror("socket"); exit(errno); } /*Initialize the address and port information of the server (the other side)*/ bzero(&dest,sizeof(dest)); dest.sin_family = AF_INET; dest.sin_port = htons(atoi(argv[2])); if(inet_aton(argv[1],(struct in_addr *)&dest.sin_addr.s_addr) == 0){ perror(argv[1]); exit(errno); } /*connect to the server*/ if(connect(sockfd,(struct sockaddr*)&dest,sizeof(dest)) !=0){ perror("connect"); exit(errno); } printf("\n---ready to chatting...---\n"); while(1){ /* Clear the collection */ FD_ZERO(&rfds); /*Add standard input handle 0 to the collection*/ FD_SET(0,&rfds); maxfd = 0; /*Add the current connection handle socket to the collection*/ FD_SET(sockfd,&rfds); if(sockfd > maxfd) maxfd = sockfd; /*Set the maximum waiting time*/ tv.tv_sec = 1; tv.tv_usec = 0; /*Start waiting*/ retval = select(maxfd+1,&rfds,NULL,NULL,&tv); if(retval == -1){ printf("select error,quit!\n"); break; }else if(retval == 0){ continue; }else{ If(FD_ISSET(sockfd,&rfds)){ /* If there is a message on the connected socket, it will be received and displayed */      bzero(buffer,MAXBUF+1); /*Receive the message sent by the other party, up to MAXBUF bytes*/ len = recv(sockfd, buffer, MAXBUF, 0); if(len > 0)     printf("recv msg success:%s! %d bytes rcv.\n ", buffer, len); else { `` if(len < 0){ printf("recv msg fail.the errno is:%d,error info is %s.\n",errno,strerror(errno)); } else   printf("quit.\n"); break; } }// FD_ISSET = sockfd case   if(FD_ISSET(0,&rfds)){ /* If the user has input, read the content and send it*/ bzero(buffer, MAXBUF+1); fgets(buffer, MAXBUF, stdin); If(!strncasecmp(buffer, "quit", 4)){ printf("self request to quit the chating\n"); break; } /*Send a message to the server*/ len = send(sockfd, buffer, strlen(buffer)-1 , 0); `` if(len < 0){ printf("mgs:%s send fail!errno is:%d,error info is:%s\n", buffer, errno, strerror(errno)); break; }else{     printf("msg: %s\t send success, send %d bytes!\n", buffer, len); } }//FD_ISSET = 0 `` }//select processing ends }//Process chat while loop /* close the connection */ close(sockfd); return 0; }

Operation result:

Terminal 1: Server

[root@localhost net]# ./select_server 7838 ----waiting for new connecting to start new char---- server: got connection from 172.31.100.236,port 59462,socked 4 recv msg success:kfldsjfk! 8 bytes rcv. 456354 msg: 456354 send success, send 6 bytes! recv msg success:453455! 6 bytes rcv.

Terminal 2: Client

[root@localhost net]# ./select_client 172.31.100.236 7838 ---ready to chatting...--- kfldsjfk msg: kfldsjfk send success, send 8 bytes! recv msg success:456354! 6 bytes rcv. 453455 msg: 453455 send success, send 6 bytes!

The above is the content of this article about how to implement select asynchronous communication under Linux. I believe everyone has a certain understanding. I hope that the content shared by the editor will be helpful to everyone.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us