UDP套接字问题
我正在编写一个多人游戏(显然使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里一个可能的问题可能是
select()
调用。我相信第一个参数需要是最高套接字号+1。One possible issue here is possibly the
select()
call. I believe the first parameter needs to be the highest socket number +1.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 toselect()
may have an empty or uninitialized fd_set. Make sure you useFD_ZERO(&readSockets)
andFD_SET(serverSocket, &readSockets)
before your loop. Also it would be good to check for errors on theselect()
call.嗯......稍微摆弄代码后,我发现了这些行:
所以,它正在接收数据,但控制台上的消息立即被删除。 [叹]
Hmmm... after fiddling with the code a bit, I found these lines:
So, it was receiving data, but the message on the console was getting erased instantly. [sigh]
您应该检查
select()
返回的错误。在 Windows 上,这将类似于:由于看起来您正在使用超时,因此
select()
也可以返回零,即没有套接字描述符准备就绪,但超时已过期。You are supposed to check for errors returned by
select()
. On Windows this would be something like:Since it looks like you are using a timeout,
select()
can also return zero, i.e. no socket descriptors are ready, but timeout expired.