我已閱讀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/)。 –