UDP套接字问题

发布于 2024-08-18 01:43:40 字数 463 浏览 9 评论 0原文

我正在编写一个多人游戏(显然使用UDP套接字。注意:使用winsock 2.2)。服务器代码如下所示:

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

虽然这没有从我的客户端接收数据,但这是:

recvfrom(serverSocket, buffer, sizeof(buffer), 0, &client, &client_size);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时间海 2024-08-25 01:43:40

这里一个可能的问题可能是 select() 调用。我相信第一个参数需要是最高套接字号+1。

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

夜司空 2024-08-25 01:43:40

FD_SET 位于循环末尾,因此看起来您第一次调用 select() 可能有一个空的或未初始化的 fd_set。确保在循环之前使用 FD_ZERO(&readSockets)FD_SET(serverSocket, &readSockets)。此外,最好检查 select() 调用中的错误。

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.

格子衫的從容 2024-08-25 01:43:40

嗯......稍微摆弄代码后,我发现了这些行:

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

所以,它正在接收数据,但控制台上的消息立即被删除。 [叹]

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]

邮友 2024-08-25 01:43:40

您应该检查 select() 返回的错误。在 Windows 上,这将类似于:

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

由于看起来您正在使用超时,因此 select() 也可以返回零,即没有套接字描述符准备就绪,但超时已过期。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文