control over keyboard and sockets (like unix's select)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: hartman (hartman_at_mixmail.com)
Date: 08/09/04


Date: Tue, 10 Aug 2004 01:03:09 +0200

Hello,

I have a server (a console type application) that listen clients through a
socket and users type commands in server through keyboard. So my server
execution it's a loop where application should be blocked waiting for data
in the socket or keys pressed by the user.

Searching in newsgroups and google, I found the WaitForMultipleObjects
function and write a first version of the possible solution, but when the
program starts it enters in an infinite loop showing "I'm the keyboard"
indefinitely, it's like always a key had been pressed. If I remove the
keyboard and wait only for the socket, it works the first time, but
following iterations shows "I'm the socket".

I would like to see an example of how could I manage sockets and keyboard
like Unix's "select" function. Below I show the source code used.

thanks in advance

Javier

    /* data arrives to socket */
    WSAEVENT datosSocketEvnt;
    // keyboard event
    HANDLE keyboardEvnt;
    /* array of events */
    HANDLE myEvents[2];

    // creates new event for socket
    datosSocketEvnt = WSACreateEvent();
    WSAEventSelect(serverSocket, datosSocketEvnt, FD_READ);
    myEvents[0] = datosSocketEvnt;

    keyboardEvnt = GetStdHandle(STD_INPUT_HANDLE);
    myEvents[1] = keyboardEvnt;

    while (key != 'E')
     switch (WaitForMultipleObjects(2, myEvents, FALSE, INFINITE)) {
        case WAIT_OBJECT_0:
            fprintf(stderr, "I'm the socket\n");

        case WAIT_OBJECT_0 + 1:
           fprintf(stderr,"I'm the keyboard\n");
       break;
    }

P.S: Source code compiles OK, I translate the variable names to English for
a best understanding of code, sorry for my very bad English



Relevant Pages