First look at a code:
Fd_set readfs,writefs; intMAXFD =-1; structtimeval TP; intNret =0; Unchar brcvbuf[2048]; Tp.tv_sec=2; Tp.tv_usec=0; Fd_zero (&Readfs); Fd_zero (&writefs); Fd_set (G_isockid,&READFS);/*G_isockid for the socket to be detected*/maxfd=Max (MAXFD, G_isockid); Maxfd+=1; Nret=Select(Maxfd, &readfs, &writefs, NULL, &TP); if(Fd_isset (G_isockid, &readfs))/*determine if there is data*/{ndsize= Recv (G_isockid, Brcvbuf,sizeof(BRCVBUF),0); }
Yesterday's look no big problem, but select in the performance of each operating system is not the same, for example, under HP, there may be no data but execution to recv situation!!!
All calls to select must be checked for their return value!!
The above code is modified as follows:
Fd_set readfs,writefs; intMAXFD =-1; structtimeval TP; intNret =0; Unchar brcvbuf[2048]; Tp.tv_sec=2; Tp.tv_usec=0; Fd_zero (&Readfs); Fd_zero (&writefs); Fd_set (G_isockid,&READFS);/*G_isockid for the socket to be detected*/maxfd=Max (MAXFD, G_isockid); Maxfd+=1; Nret=Select(Maxfd, &readfs, &writefs, NULL, &TP); if (Nret < 0) {return; } if(Fd_isset (G_isockid, &readfs))/*determine if there is data*/{ndsize= Recv (G_isockid, Brcvbuf,sizeof(BRCVBUF),0); }
Lessons of SELECT function in Network programming