【发布时间】:2019-10-05 09:10:34
【问题描述】:
我正在我的 TCP 服务器中处理一个函数,该函数在定义的时间段内接受预定数量的客户端连接(我现在将其设置为 10 秒),并在达到预定连接数时返回。我正在使用 select 函数使服务器超时,但由于某种原因,每次客户端加入时超时都会重置。例如,如果客户端在 5 秒后加入服务器,则超时重置并从 10 开始重新倒计时。非常感谢您的帮助,谢谢。
我正在使用 mac,我认为在这个操作系统上 FD_ISSET 用于检查客户端是否已连接(我很确定在 Linux 上不需要)。所以你可以像对待 linux 上 select 函数的返回值一样对待这个函数的返回值。
while (num_conn < NO_OF_CLIENTS) {
// Listen for clients
err = listen(server_fd, 128);
if (err < 0) {
fprintf(stderr, "Could not listen on socket\n");
exit(EXIT_FAILURE);
}
// Zero out memory for the client information
memset( & client, 0, sizeof(client));
socklen_t client_len = sizeof(client);
FD_ZERO( & set);
FD_SET(server_fd, & set);
select(server_fd + 1, & set, NULL, NULL, & timeout);
// server times out after allowing 30 seconds for clients to join.
// If no clients join function returns 0. Otherwise returns no_clients
nready = FD_ISSET(server_fd, & set);
printf("nready is: %d\n", nready);
if (nready == 0) {
return num_conn; // returns number of connections if a time out occurs.
}
// Accept the connection from the client
client_fd[num_conn] = accept(server_fd, (struct sockaddr * ) & client, & client_len);
if (client_fd < 0) {
fprintf(stderr, "Could not establish new connection\n");
// SEND REJECT??
exit(EXIT_FAILURE);
}
// Assign value to number of clients here and let it set after a time out
// more work to be done here
printf("Accepted connection from client %d\n", num_conn);
num_conn++;
}
【问题讨论】:
-
不清楚您使用的是哪个操作系统,但来自man select:“在 Linux 上,select() 修改超时以反映未睡眠的时间量;大多数其他实现不要这样做。(POSIX.1 允许任何一种行为。)"。看起来您依赖于未普遍实施的特定非标准行为。
-
我正在使用 mac,我认为在这个操作系统上 FD_ISSET 用于检查客户端是否已连接(我很确定在 Linux 上不需要)。所以你可以像对待 linux 上 select 函数的返回值一样对待这个函数的返回值。
-
如果您需要为每个套接字分别设置超时,您需要为它们设置某种优先级队列并为
select选择最低超时
标签: c select tcp server timeout