Running the select() function (Line 13 of provided code) returns error 10022 (WSAEINVAL) meaning either my timeout variable in negative or all 3 other variables are NULL. But I only get this error after the first iteration through the outside loop.
Basically my set up is send a window of packets of size N at base, then check to see if any confirmations have been sent back to me by checking if my socket is ready to be read from. For the first window of packets, the select() call works just fine, returning 1. But all calls to select() after the first iteration of the full loop result in the error code. I'm completely out of ideas.
I saw a thread saying to just wrap the function with another function call and it didn't seem to change the problem. I have no idea why it would work the first loop but not at all after that.
int Exit = 0;
int endRecv = 0;
int ready = 0;
//Sending Algorithm
int base = 0;
uint32_t confirmedPosition;
while (Exit == 0) {
//Send data in an N sized Window.
for (int i = 0; i < N; i++) {
if (successfulPackets[i + base] != 1) {
memcpy(&buffer_tx, &packets[i + base], BUFFERS_LEN);
sendto(socketS, buffer_tx, BUFFERS_LEN, 0, (sockaddr*)& addrDest, sizeof(addrDest));
}
}
endRecv = 0;
//Receive Confirmations
while (endRecv == 0) {
//Poll to see if something is waiting
ready = select(NULL, &readfds, NULL, NULL, &pollTime);
if (ready == -1) {
printf("Error with Code: %d", WSAGetLastError());
} else if (ready == 1) {//If yes, log confirmation
recvfrom(socketS, buffer_rx, BUFFERS_LEN, 0, (sockaddr *)&from, &fromlen);
memcpy(&confirmedPosition, &buffer_rx, 4);
successfulPackets[confirmedPosition] = 1;
fileSize -= BUFFERS_LEN - 8;
} else {//If no, end receiving and send
endRecv = 1;
}
}
//Apply Changes to Base
for (int i = 0; i < N; i++) {
if (successfulPackets[base] == 1) {
base++;
}
}
if (fileSize <= 0) {
Exit = 1;
}
}