欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

close和SO_LINGER

发布时间:2025/6/15 编程问答 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 close和SO_LINGER 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

              close函数的作用是关闭套接字,并终止TCP连接。unix网络编程这本书上是这样说的,我觉得这个解释有人会让人产生误解。close了某个socket,该socket就真的必须关闭吗?其实不是,close是将该套接字的引用计数减1,当某个套接字的引用计数为0时,该套接字就被关闭了;不为0,就不会被关闭。多进程并发服务器中会出现这种情况,我开始就误解了。

              SO_LINGER套接字选项是用来设置close操作的。直接看代码吧。

 

[mapan@localhost test]$ ls client.cpp makefile server.cpp [mapan@localhost test]$ cat server.cpp #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <errno.h> #include <malloc.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <stdarg.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #define MAXLINE 4096int main() {int listenfd,acceptfd;socklen_t clilen;struct sockaddr_in cliaddr,servaddr;listenfd=socket(AF_INET,SOCK_STREAM,0);servaddr.sin_family=AF_INET;servaddr.sin_port=htons(8888);servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); bind(listenfd,(struct sockaddr *)&servaddr,sizeof(struct sockaddr_in));listen(listenfd,5);acceptfd=accept(listenfd,(struct sockaddr *)NULL,NULL);char recvbuf[200000];while(1){getchar();read(acceptfd,recvbuf,sizeof(recvbuf)); }getchar();close(listenfd);return 0; } [mapan@localhost test]$ cat client.cpp #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <errno.h> #include <malloc.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <stdarg.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #define MAXLINE 4096int main() {int sockfd;struct sockaddr_in servaddr;sockfd=socket(AF_INET,SOCK_STREAM,0);bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=htons(8888);servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");struct linger so_linger;so_linger.l_onoff=0;//so_linger.l_linger=20;setsockopt(sockfd,SOL_SOCKET,SO_LINGER,&so_linger,sizeof(so_linger)); int ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));char sendbuf[200000];write(sockfd,sendbuf,sizeof(sendbuf)); close(sockfd);return 0; }[mapan@localhost test]$ cat makefile all:server clientserver.o:server.cppg++ -c server.cpp client.o:client.cppg++ -c client.cpp server:server.og++ -o server server.o client:client.og++ -o client client.oclean:rm -f server client *.o [mapan@localhost test]$

 

编译并运行,客户端需要打开另一个窗口执行。

 

[mapan@localhost test]$ make g++ -c server.cpp g++ -o server server.o g++ -c client.cpp g++ -o client client.o [mapan@localhost test]$ ./server

 

运行客户端,并查看网络状态。

 

[mapan@localhost test]$ ./client [mapan@localhost test]$ [mapan@localhost ~]$ netstat -na | grep 8888 tcp 0 0 127.0.0.1:8888 0.0.0.0:* LISTEN tcp 0 61101 127.0.0.1:35260 127.0.0.1:8888 FIN_WAIT1 tcp 138900 0 127.0.0.1:8888 127.0.0.1:35260 ESTABLISHED [mapan@localhost ~]$

 

 

可以看到的是close立即返回了,套接字关闭了,但客户端发送缓冲区中仍然还有数据。当服务端接收缓冲区有地方后,这些数据将会由系统自动发送给服务端,但是此时客户端讲不会管服务端是否已接收到数据。l_onoff=0,就是关闭这个套接字选项,默认close操作。注意观察,此时客户端的状态时FIN_WAIT1,就是客户单还没有把数据发送完毕,所以没有接到服务端协议栈返回的ACK,所以客户端为这个状态。

 

再改变客户端代码:

 

#include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <stdarg.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #define MAXLINE 4096int main() {int sockfd;struct sockaddr_in servaddr;sockfd=socket(AF_INET,SOCK_STREAM,0);bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=htons(8888);servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");struct linger so_linger;so_linger.l_onoff=1;so_linger.l_linger=0;setsockopt(sockfd,SOL_SOCKET,SO_LINGER,&so_linger,sizeof(so_linger)); int ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));char sendbuf[200000];write(sockfd,sendbuf,sizeof(sendbuf)); getchar();close(sockfd);return 0; }


还是按照上述操作,编译后启动服务端和客户端。此时会发现客户端卡在getchar()处,查看此时的网络状态。

 

 

[mapan@localhost ~]$ netstat -na | grep 8888 tcp 0 0 127.0.0.1:8888 0.0.0.0:* LISTEN tcp 138900 0 127.0.0.1:8888 127.0.0.1:35262 ESTABLISHED tcp 0 61100 127.0.0.1:35262 127.0.0.1:8888 ESTABLISHED [mapan@localhost ~]$

 

 

 

客户端的发送缓冲区中还没有数据发送出去,如果是我们说的第一种情况,在客户端按下回车键之后,客户端应该FIN_WAIT1状态。好,我们在客户端按下回车键后看网络状态。

 

[mapan@localhost ~]$ netstat -na | grep 8888 tcp 0 0 127.0.0.1:8888 0.0.0.0:* LISTEN [mapan@localhost ~]$

 

 

看,连接直接断开了。说明close调用后,会丢弃发送缓冲区的内存,并发送一个RST给服务端,从而断开连接,这也避免了time_wait的状态。这也是将 so_linger.l_onoff=1,so_linger.l_linger=0的close效果。

 

在看客户端代码:

 

[mapan@localhost test]$ cat client.cpp #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <errno.h> #include <malloc.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <stdarg.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> #define MAXLINE 4096int main() {int sockfd;struct sockaddr_in servaddr;sockfd=socket(AF_INET,SOCK_STREAM,0);bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=htons(8888);servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");struct linger so_linger;so_linger.l_onoff=1;so_linger.l_linger=10;setsockopt(sockfd,SOL_SOCKET,SO_LINGER,&so_linger,sizeof(so_linger)); int ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));char sendbuf[200000];write(sockfd,sendbuf,sizeof(sendbuf)); //getchar();close(sockfd);return 0; }


还是重复上述操作,编译运行查看网络状态,此时我将getchar()注释掉了。你会看到客户端会卡在那里,其原因是调用了close函数,但是它不会马上返回,close等待的时间是我们设置的超时时间。看此时的网络状态。

 

 

[mapan@localhost ~]$ netstat -na | grep 8888 tcp 0 0 127.0.0.1:8888 0.0.0.0:* LISTEN tcp 0 61101 127.0.0.1:35266 127.0.0.1:8888 FIN_WAIT1 tcp 138900 0 127.0.0.1:8888 127.0.0.1:35266 ESTABLISHED [mapan@localhost ~]$

 

 

 

当客户端发送缓冲区中的数据全部发送到服务端的协议栈,并且接收到了服务端的ACK,那么此时close就会返回,前提是在我们设置的超时时间之内。过了超时时间,close也会返回,那就和我们说的第一种情况一样了。

网络问题本应该用tcpdump抓包来看效果的,但是很遗憾,我正在测试的linux上没有root权限。

 

 

参考资料:unix网络编程卷一

 


 

 

 

 

 

 

 

 

 

总结

以上是生活随笔为你收集整理的close和SO_LINGER的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。