Jaden DeFields
Jaden DeFields

Reputation: 23

Winsock2: select() function giving "invalid arguement error" (Error 10022)?

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;
    }
}

Upvotes: 2

Views: 532

Answers (1)

Paolo Brandoli
Paolo Brandoli

Reputation: 4750

When select returns 0 then the content of the structure readfds is also set to zero (select modifies it to indicate which socket is ready).

When the outer loop executes again then the readfds structure does not contain any socket to check, which causes the error. You should set again the socket in readfds.

Upvotes: 3

Related Questions