Question

I would like to preface this by saying my problem is solved completely by removing the timeout option of Select() by setting it to NULL.

But i want to use a timeout, so here is my problem. The server program is very simply&plain, it listens for connections, accepts them and prints to the console.

The problem is that after the first timeout on select, select will return -1 (MSDN gives me little-to-no documentation as to what would cause -1 to be returned from select(), only that it an error signal) and effectively stop any more communication from the program.

Server - main.cpp

int main(){

    /* variables */
    int iResult, iSendResult;
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;
    bool exit=false;
    int foundRoom, tempFoundRoom;
    fd_set readfds, writefds;
    struct timeval tv;
    long double counter=0;
    int maxfd=0;

    /* Server variables */
    WSADATA wsaData;
    SOCKET ListenSocket = INVALID_SOCKET;
    SOCKET ClientSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL;
    struct addrinfo hints;


    printf("~SERVER~\n");

     /* Initialize Winsock*/
    initialize_winsock(wsaData, hints, &result);

    /* Set socket and bind to TCP listening socket */
    set_socketandbind(ListenSocket, result, maxfd);

    //non-blocking mode
    u_long iMode=1;
    ioctlsocket(ListenSocket,FIONBIO,&iMode);
    ioctlsocket(ClientSocket,FIONBIO,&iMode);

    // clear the set ahead of time
    FD_ZERO(&readfds);
    //FD_ZERO(&writefds);

    //add our descriptors to the set
    FD_SET(ListenSocket, &readfds);

    //wait until either socket has data ready to be recv()d (timeout 5 secs)
    tv.tv_sec = 5;
    tv.tv_usec = 0;

    /* main loop */
    for(;;){
        cout << "Maxfd: " << ListenSocket << " \ " << maxfd << endl;

        //select
        int rv = select(ListenSocket+1, &readfds, NULL, NULL, &tv);

        cout << "Updateing... rv: " << rv << endl;

        if (rv == -1) {
            perror("Select: "); // error occurred in select()
        }else if (rv == 0){
            printf("Timeout occurred! No data after 5 seconds.\n");
        }else{

            // one or both of the descriptors have data
            if (FD_ISSET(ListenSocket, &readfds)) {

                // Accept a client socket
                ClientSocket=accept(ListenSocket, NULL, NULL);

                cout << "Connected a user." << endl;

            }

            counter++;
        }

        cout << endl;
        Sleep(500);

    };

    /* Unset server socket */
    closesocket(ListenSocket);

    system("pause");
    return 0;
}

The output should be, timeout, timeout, timeout, because thats how select() is supposed to work, and how it works on my client. Instead the output is timeout, error, error, error:

enter image description here

Was it helpful?

Solution

You need to reset 'TV' and the FD structures every time around the loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top