views:

180

answers:

4

I'm writing a multiplayer game (obviously using UDP sockets. note: using winsock 2.2). The server code reads something like this:

while(run)
{
    select(0, &readSockets, NULL, NULL, &t)
    if(FD_ISSET(serverSocket, &readSockets))
    {
        printf("%s\n","Data receieved");
        //recvfrom over here
    }
    FD_SET(serverSocket, &readSockets);
}

While this is not receiving data from my client, this is:

recvfrom(serverSocket, buffer, sizeof(buffer), 0, &client, &client_size);
+1  A: 

One possible issue here is possibly the select() call. I believe the first parameter needs to be the highest socket number +1.

Mark Wilkins
and how do you get that?
Aviral Dasgupta
Quoting the winsock documentation : Parameter 1 : nfds - included for the sake of backwards compatibility
Aviral Dasgupta
You will need that it you want it to work on non-Windows systems. With just one socket it would be `serverSocket + 1`.
mark4o
Windows select ignores first parameter - it's in msdn docs.
Nikolai N Fetissov
+1  A: 

The FD_SET is at the end of the loop so it looks like your first call to select() may have an empty or uninitialized fd_set. Make sure you use FD_ZERO(&readSockets) and FD_SET(serverSocket, &readSockets) before your loop. Also it would be good to check for errors on the select() call.

mark4o
Yep... I'm already doing that...
Aviral Dasgupta
So is `select()` returning an error, or is it not returning?
mark4o
No, it isn't returning an errors
Aviral Dasgupta
A: 

Hmmm... after fiddling with the code a bit, I found these lines:

console->clear();
console->resetCursorPosition();

So, it was receiving data, but the message on the console was getting erased instantly. [sigh]

Aviral Dasgupta
A: 
Nikolai N Fetissov