【发布时间】:2016-05-01 00:25:02
【问题描述】:
我正在构建一个向服务器发送请求的应用程序,我需要实现一种方法来控制我尝试 connect() 到服务器的时间。
我想在十秒后结束 connect() 函数。我已经阅读了 select() 和非阻塞套接字,但我并不完全理解它是如何工作的。
谁能给我一个非常简单的例子来说明如何做到这一点?使用 C。谢谢。
另外,我还想为 send 和 recv 函数添加相同的超时。
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
int main(void) {
struct sockaddr_in server;
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server.sin_addr.s_addr = inet_addr("10.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(atoi("80"));
fcntl(sockfd, F_SETFL, O_NONBLOCK);
struct timeval tv;
fd_set writefds;
tv.tv_sec = 5;
tv.tv_usec = 500000;
FD_ZERO(&writefds);
FD_SET(sockfd, &writefds);
connect(sockfd, (struct sockaddr *)&server, sizeof(server));
if (errno == EINPROGRESS)
{
printf("In progress");
}
select(sockfd+1, NULL, &writefds, NULL, &tv);
你能解释一下为什么 errno 的 if 条件直到超时后才显示?这对我来说没有意义。是否有任何额外的代码我应该在这里查看以正确处理这个问题。谢谢。
【问题讨论】:
-
除非先前的系统调用返回-1,否则测试
errno是不正确的。您需要以这种方式测试所有系统调用:socket(), fcntl(), connect(), select(), ... -
好的,谢谢。知道为什么在连接之前或之后的 printf 语句在超时成功之前不会打印。我只是对发生的事情感到困惑?
-
因为
fcntl()失败了,你没有检查错误,所以没有发现,所以socket仍然处于阻塞模式。当有人指出您的代码中存在重大故障时,“OK”并不是一个充分的回应。您应该更正并重新测试。 -
让我尽快回复您一些更新的代码。另外,我不相信 fcntl 会失败,因为包含它时 connect 会立即退出。但我会看到的。
-
我不会说没有错误检查被认为是我的代码中的一个主要缺陷,尽管它仍然是一个缺陷。为什么这里的人总是有聪明的说法?
标签: c sockets networking