开发者

UDP socket problem

I'm writing a multiplayer game (obviously using UDP sockets. note: using winsock 2.2). 开发者_StackOverflow中文版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);


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


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.


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]


You are supposed to check for errors returned by select(). On Windows this would be something like:

if (( nret = select( nfds, &rset, &wset, &eset, &to )) == SOCKET_ERROR )
{
   // error handling, probably with WSAGetLastError()
   // ...
}

Since it looks like you are using a timeout, select() can also return zero, i.e. no socket descriptors are ready, but timeout expired.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜