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 4096



int 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 4096


int 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 client

server.o:server.cpp
	g++ -c server.cpp
client.o:client.cpp
	g++ -c client.cpp
server:server.o
	g++ -o server server.o
client:client.o
	g++ -o client client.o

clean:
	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 4096


int 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 4096


int 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网络编程卷一

 


 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盼盼编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值