While (1) {
Rset=allset;
Printf (" wait... \n");
Nready=select (maxfd + 1, & amp; Rset, NULL, NULL, NULL);//nready is received several concern
If (nready==1) {
If (errno==EINTR)
continue;
ERR_EXIT (" select ");
}
If (nready==0)
continue;
If (FD_ISSET (listenfd, & amp; Rset)) {
Peerlen=sizeof (peeraddr);
Conn=accept (listenfd, (struct sockaddr *) & amp; Peeraddr, & amp; Peerlen);
Printf (" conn==% d \ n ", conn);
If (conn==1)
ERR_EXIT (" accept ");
for (i=0; I & lt; FD_SETSIZE; I++) {//to find an empty position to prevent the conn
If (client [I]==1) {
The client [I]=conn.
If (I & gt; Maxi)
Maxi=I;
break;
}
}
If (I==FD_SETSIZE) {//can't find the empty position to prevent the conn
Fprintf (stderr, "many clients");
The exit (EXIT_FAILURE);
}
Printf (" IP port=% d=% s \ n ", inet_ntoa (peeraddr. Sin_addr), ntohs (peeraddr. Sin_port));
FD_SET (conn, & amp; Allset);
If (conn & gt; Maxfd)
Maxfd=conn.
If (- nready & lt;=0)//if the event number==0, then returned directly
Printf (" duqujieshu \ n ");
continue;
}
for (i=0; I & lt;=maxi; I++) {
Conn=client [I];
If (conn==1)
continue;
If (FD_ISSET (conn, & amp; Rset));//in the collection, it must produce the data
{
Printf (" conn=% d \ n ", conn);
Char recvbuf [1024]={0};
Memset (recvbuf, 0, sizeof (recvbuf));
Int ret=readline (conn, recvbuf, 1024);//now, the question is, if I opened the two clients, such as their conn is 5 and 6, so if I when the client sends data 6, because of the order, will first enter the 5, then stuck in the input buffer read 5
If (ret==1)
ERR_EXIT (" readline ");
If (ret==0)
{
Printf (" the client close \ n ");
The client [I]=1;
FD_CLR (conn, & amp; Allset);
}
The fputs (recvbuf, stdout);
Writen (conn, recvbuf, strlen (recvbuf));
If (- nready & lt;=0)
break;
}
}
}
CodePudding user response:
This is what network programming, 5 connection didn't release?CodePudding user response:
Is the select functionNready=select (maxfd + 1, & amp; Rset, NULL, NULL, NULL);
Nready to return to my attention by the number of file descriptors
First of all, is now two client connection -- -- -- -- -- 5 and 6
If I were in the client 6 write data
So should be accepted by the select through a interface to
At this point the select end block
Nready return 1
But at this point I shouldn't the rset of only 6
?
But when the conn=5
FD_ISSET (conn, & amp; Rset) can also be successful
But normally not should fail
Only FD_ISSET (6, & amp; Rset) to succeed?