I want to make a non-blocking send() and recv() with select() and FD_ISSET(). I run into this behavior in which FD_ISSET() will be true for the first socket, and all other sockets are always not ready. I am confused why that is happening and I am wondering if I am using select() properly.
If I sent 100 requests, eventually one of the socket other than the first will be ready to recv(), but I am not getting that behavior.
for(int i = 0; i < P - 1; i++) {
sockArr[i] = GetSocket(server, port);
if (sockArr[i] < 0) {
// handle error
}
fcntl(sockArr[i], F_SETFL, O_NONBLOCK);
if(sockArr[i] > maxfd) {
maxfd = sockArr[i];
}
}
fd_set sockSet;
for(int i = 0; i < P - 1; i++) {
numBytes = send(sockArr[i], request, requestLen, 0);
if (numBytes < 0 || numBytes != requestLen) {
// handle error
}
// read from other stackoverflow post you need to rest
// per select() call
FD_ZERO(&sockSet);
for(int i = 0; i < P - 1; i++) {
FD_SET(sockArr[i], &sockSet);
}
if(select(maxfd+1, &sockSet, NULL, NULL, NULL)) {
for(int j = 0; j < i; j++) {
if(FD_ISSET(sockArr[j], &sockSet)) { // <------ only true for j = 0
numBytes = recv(sockArr[j], buffer, MAXBUFLEN - 1, 0);
if (numBytes < 0) {
// handle error
}
}
}
}
}
selectinside aforloop? That seems weird. Are you really intending to do onesendfollowed by aselect, and then twosendfollowed by aselect, and so on? Or did you mean to do all thesendcalls followed by a singleselect? - kaylumselect. - kaylumO_NONBLOCKonly affects the actual IO operationsread,write,send,recv, etc. It doesn't affectselect. It wouldn't make sense if it did. What is the point ofselectif it doesn't block? In that case you may as well just immediately callrecvand check its return value to see if any data was read. - kaylumforloop. Then you have a singleselectcall inside either an infinete loop or a loop that exits on an appropriate condition. After theselectcall you have exactly therecvcode that you have now (including its enclosing for loop). - kaylum