我已阅读TCP option SO_LINGER (zero) - when it's required和其他几个相关的问题和答案,但我无法复制这些帖子中解释的任何SO_LINGER
行为。我将在这里分享我的许多实验之一。为什么不是SO_LINGER选项有0超时或10秒超时而不是立即或10秒后移除套接字?
我在以下环境中执行此实验。
$ lsb_release -d
Description: Debian GNU/Linux 9.0 (stretch)
$ gcc -dumpversion
6.3.0
这里是一个错误操作的客户端连接到服务器, 但没有接收到任何数据进行90秒的实例。
/* client.c */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
int main()
{
int sockfd;
int ret;
struct addrinfo hints, *ai;
char buffer[256];
ssize_t bytes;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if ((ret = getaddrinfo(NULL, "8000", &hints, &ai)) == -1) {
fprintf(stderr, "client: getaddrinfo: %s\n", gai_strerror(ret));
return 1;
}
sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sockfd == -1) {
perror("client: socket");
return 1;
}
if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) == -1) {
perror("client: connect");
close(sockfd);
return -1;
}
printf("client: connected\n");
/*
bytes = recv(sockfd, buffer, sizeof buffer, 0);
if (recv(sockfd, buffer, sizeof buffer, 0) == -1) {
perror("client: recv");
close(sockfd);
return -1;
}
printf("client: received: %.*s\n", (int) bytes, buffer);
*/
sleep(90);
freeaddrinfo(ai);
printf("client: closing socket ...\n");
close(sockfd);
printf("client: closed socket!\n");
return 0;
}
这里是发送hello
到连接 到服务器的客户端,然后立即关闭连接我的服务器代码。此服务器是 为简单起见不是多线程。在一个多线程服务器中,它将接受来自客户端的100多个连接的连接,其中很多可能会行为不端,我们的目标是尽快放弃 无用的套接字,以释放为这些套接字所使用的端口。
为了达到这个目的,我们启用了SO_LINGER
套接字选项,其延迟时间为10秒,延迟时间为 。
/* server.c */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
int main()
{
int sockfd;
int ret;
int yes = 1;
struct addrinfo hints, *ai;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((ret = getaddrinfo(NULL, "8000", &hints, &ai)) == -1) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
return 1;
}
sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sockfd == -1) {
perror("server: socket");
return 1;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) == -1) {
perror("server: setsockopt");
close(sockfd);
return 1;
}
if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) == -1) {
perror("server: bind");
close(sockfd);
return 1;
}
freeaddrinfo(ai);
if (listen(sockfd, 10) == -1) {
perror("server: listen");
close(sockfd);
return 1;
}
printf("server: listening ...\n");
while (1) {
int client_sockfd;
struct sockaddr_storage client_addr;
socklen_t client_addrlen = sizeof client_addr;
struct linger l_opt;
printf("server: accepting ...\n");
client_sockfd = accept(sockfd, (struct sockaddr *) &client_addr,
&client_addrlen);
/* Set SO_LINGER opt for the new client socket. */
l_opt.l_onoff = 1;
l_opt.l_linger = 10;
setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &l_opt, sizeof l_opt);
if (client_sockfd == -1) {
perror("server: accept");
continue;
}
if (send(client_sockfd, "hello\n", 6, 0) == -1) {
perror("server: send");
continue;
}
printf("server: sent: hello\n");
printf("server: closing client socket ...\n");
close(client_sockfd);
printf("server: closed client socket!\n");
}
return 0;
}
这是我的实验跑步者。
# run.sh
gcc -std=c99 -Wall -Wextra -Wpedantic -D_DEFAULT_SOURCE server.c -o server
gcc -std=c99 -Wall -Wextra -Wpedantic -D_DEFAULT_SOURCE client.c -o client
./server &
sleep 1
./client
pkill ^server$
在另一窗口/终端,我运行这个小bash脚本来监控每10秒的套接字的 状态。
$ for i in {1..10}; do netstat -nopa 2> /dev/null | grep :8000; echo =====; sleep 10; done
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 16293/./server off (0.00/0/0)
tcp 7 0 127.0.0.1:35536 127.0.0.1:8000 CLOSE_WAIT 16295/./client off (0.00/0/0)
tcp 0 0 127.0.0.1:8000 127.0.0.1:35536 FIN_WAIT2 - timewait (59.84/0/0)
=====
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 16293/./server off (0.00/0/0)
tcp 7 0 127.0.0.1:35536 127.0.0.1:8000 CLOSE_WAIT 16295/./client off (0.00/0/0)
tcp 0 0 127.0.0.1:8000 127.0.0.1:35536 FIN_WAIT2 - timewait (49.83/0/0)
=====
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 16293/./server off (0.00/0/0)
tcp 7 0 127.0.0.1:35536 127.0.0.1:8000 CLOSE_WAIT 16295/./client off (0.00/0/0)
tcp 0 0 127.0.0.1:8000 127.0.0.1:35536 FIN_WAIT2 - timewait (39.82/0/0)
=====
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 16293/./server off (0.00/0/0)
tcp 7 0 127.0.0.1:35536 127.0.0.1:8000 CLOSE_WAIT 16295/./client off (0.00/0/0)
tcp 0 0 127.0.0.1:8000 127.0.0.1:35536 FIN_WAIT2 - timewait (29.81/0/0)
=====
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 16293/./server off (0.00/0/0)
tcp 7 0 127.0.0.1:35536 127.0.0.1:8000 CLOSE_WAIT 16295/./client off (0.00/0/0)
tcp 0 0 127.0.0.1:8000 127.0.0.1:35536 FIN_WAIT2 - timewait (19.80/0/0)
=====
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 16293/./server off (0.00/0/0)
tcp 7 0 127.0.0.1:35536 127.0.0.1:8000 CLOSE_WAIT 16295/./client off (0.00/0/0)
tcp 0 0 127.0.0.1:8000 127.0.0.1:35536 FIN_WAIT2 - timewait (9.78/0/0)
=====
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 16293/./server off (0.00/0/0)
tcp 7 0 127.0.0.1:35536 127.0.0.1:8000 CLOSE_WAIT 16295/./client off (0.00/0/0)
tcp 0 0 127.0.0.1:8000 127.0.0.1:35536 FIN_WAIT2 - timewait (0.00/0/0)
=====
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 16293/./server off (0.00/0/0)
tcp 7 0 127.0.0.1:35536 127.0.0.1:8000 CLOSE_WAIT 16295/./client off (0.00/0/0)
=====
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 16293/./server off (0.00/0/0)
tcp 7 0 127.0.0.1:35536 127.0.0.1:8000 CLOSE_WAIT 16295/./client off (0.00/0/0)
=====
=====
上述输出显示服务器套接字(第三行中的输出的每次迭代),保持在FIN_WAIT2
状态60秒(即,默认TIMEWAIT)。
为什么SO_LINGER
选项与10
秒超时不保证服务器关闭了客户端套接字(即本地地址= 127.0.0.1:8000;外部地址= 127.0.0.1:35536)成功地在10秒后?
注意:即使有0超时,即使用以下代码,本地地址= 127.0.0.1:8000和外部地址= 127.0.0.1:35536的套接字仍保持在FIN_WAIT2
状态60秒。
/* Set SO_LINGER opt for the new client socket. */
l_opt.l_onoff = 1;
l_opt.l_linger = 0;
setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &l_opt, sizeof l_opt);
如果SO_LINGER
对去除插座或FIN_WAIT2
超时没有效果,那么真正是SO_LINGER
的目的是什么?
请参阅描述POSIX [套接字](http://pubs.opengroup)部分中的POSIX [使用选项](http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10_16)组织/ onlinepubs/9699919799 /功能/ V2_chap02.html#tag_15_10)。 –
@JonathanLeffler POSIX文档没有提及'TIME_WAIT',但几乎关于'SO_LINGER'的所有关于StackOverflow的讨论,包括我已经链接的问题和答案都似乎谈论'TIME_WAIT'。如果'SO_LINGER'对'TIME_WAIT'没有影响,这些现有答案是否被误导? –
是的; POSIX文档在任何地方都不包含“TIME_WAIT”字样。如果您想了解相关信息,则必须在其他地方搜索信息。也许你可以得到W. Richard Stevens,Bill Fenner,Andrew M. Rudoff [UNIX®网络编程,第1卷:套接字网络API,第3版](http://www.amazon.com/Unix-Network -Programming-Volume-Networking/dp/0131411551) - 又名 [UNIX®网络编程,第1卷:套接字网络API](http://unpbook.com/)。 –