错误:Socket Select() 函数总是返回零..?
谁能告诉我为什么下面的代码总是返回 0 。套接字描述符值为 3。 我正在使用 open suse TFTP 服务器。它正在侦听本地主机中的端口 69。
connect() 函数返回成功..
connection_timer.tv_sec = 2; // s
connection_timer.tv_usec = 0;
FD_ZERO(&fd_reader);
// laukiam, kol bus ka nuskaityti
FD_SET(socket_descriptor, &fd_reader);
int select_ready = select(socket_descriptor + 1, &fd_reader, NULL, NULL, &connection_timer);
当我使用 TCPdump 检查数据包时,它发送第一个数据包,然后在收到 Ack 之前在某处关闭连接。
can any one tell me why the following code always return 0 . the socket descriptor value is 3.
i am using the open suse TFTP server . which is listening on port 69 in Local host.
connect() function return success ..
connection_timer.tv_sec = 2; // s
connection_timer.tv_usec = 0;
FD_ZERO(&fd_reader);
// laukiam, kol bus ka nuskaityti
FD_SET(socket_descriptor, &fd_reader);
int select_ready = select(socket_descriptor + 1, &fd_reader, NULL, NULL, &connection_timer);
When i use TCPdump to check the packet it send the first packet then the connection is closed in somewhere before receive Ack received..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果计时器(示例中的
connection_timer
)在任何描述符变得有趣之前到期,您将从select
获得0
返回代码。所以这不是一个错误。看起来很可能您没有正确初始化
connection_timer
。You will get a return code of
0
fromselect
it the timer (connection_timer
in your example) expires before any descriptor has become interesting.So it's not an error. It seems most likely you didn't initialize
connection_timer
properly.我怀疑您没有收到响应,因为您在 UDP 套接字上使用了
connect()
,这使得您只接受来自连接目标的数据报。由于 TFTP 回复不是来自端口 69,而是来自临时端口,因此永远不会收到确认。
解决方案:在完成初始连接之前,不要
connect()
您的 UDP 套接字。I suspect that you are not receiving the response because you used
connect()
on a UDP socket, which made it so that you only accept datagrams from the connected destination.Since the TFTP reply does not come from port 69, but rather from an ephemeral port, the acknowledgement is never received.
Solution: Don't
connect()
your UDP socket until after you finish the initial connection.需要调用 WSAStartup 函数。
我确实有同样的问题,并且在调用此启动函数后得到解决。
WSAStartup functions needs to be called.
I do have the same problem and that got resolved after calling this startup function.