Why does muduo's shutdown () Fail to directly close the TCP connection?

Source: Internet
Author: User

Chen Shuo (giantchen_at_gmail)

Blog.csdn.net/solstice

Full muduo SeriesArticleList: http://blog.csdn.net/Solstice/category/779646.aspx

I received a letter from a netizen today:

In the daytime example in simple, the server calls the following function sequence when it closes the connection. Isn't this just about closing the write operation on the connection? How does it close the entire connection?

 
1: VoidDaytimeserver: onconnection (ConstMuduo: Net: tcpconnectionptr & conn)

 
2:{

 
3:If(Conn-> connected ())

4:{

 
5:Conn-> send (timestamp: Now (). toformattedstring () +"\ N");

 
6:Conn-> Shutdown ();

 
7:}

 
8:}

 
9:

 
10: VoidTcpconnection: Shutdown ()

11:{

 
12:If(State _ = kconnected)

 
13:{

 
14:Setstate (kdisconnecting );

 
15:Loop _-> runinloop (boost: BIND (& tcpconnection: shutdowninloop,This));

 
16:}

 
17:}

 
18:

 
19: VoidTcpconnection: shutdowninloop ()

 
20:{

 
21:Loop _-> assertinloopthread ();

 
22:If(! Channel _-> iswriting ())

 
23:{

24:// We are not writing

 
25:Socket _-> shutdownwrite ();

 
26:}

 
27:}

 
28:

 
29: VoidSocket: shutdownwrite ()

 
30:{

31:Sockets: shutdownwrite (sockfd _);

 
32:}

 
33:

 
34: VoidSockets: shutdownwrite (IntSockfd)

 
35:{

 
36:If(: Shutdown (sockfd, shut_wr) <0)

 
37:{

 
38:Log_syserr <"Sockets: shutdownwrite";

 
39:}

 
40:}

Chen Shuo replied as follows:

Muduo tcpconnection does not provide close, but only provides shutdown, so as to send and receive data integrity.

TCP is a full-duplex protocol. The same file descriptor is both readable and writable. shutdownwrite () closes the connection in the "write" direction and retains the "read" direction, this is called TCP half-close. If you directly close (socket_fd), socket_fd cannot be read or written.

The effect of using shutdown instead of close is that if the data has been sent by the other party and the data is still "on the road", muduo will not miss the data. In other words, muduo solved "when you plan to close the network connection, how do you know if the other party has sent some data and you have not received it ?" This problem. Of course, this problem can also be solved at the above Protocol layer. The two parties will discuss the issue so that they can directly disconnect the connection without sending data to each other.

Muduo divides "active connection close" into two steps. If the connection is to be closed, it first closes the local "write" end. After the other party closes the connection, then shut down the local "read" side. Exercise: ReadCode, "What is the behavior of muduo if the connection is closed passively ?" Tip: muduo calls back connection callback when read () returns 0, so that the client code will know that the other party is disconnected.

Muduo is also required for the method of closing the connection, that is, after the other party reads () to 0 bytes, it will take the initiative to close the connection (whether shutdownwrite () or close (), the general networkProgramThis is not a problem. Of course, there is a potential security vulnerability in this process. If the other party intentionally does not close the connection, the muduo connection will remain half open, consuming system resources.

The complete process is: we have sent the data,So shutdownwrite sends a tcp fin Shard, and the other party reads 0 bytes. Then the other party usually closes the connection, so that muduo reads 0 bytes, and muduo closes the connection. (THINKING question: after Shutdown (), the interval between muduo callback connection callback is about a round-trip time. Why ?)

In addition, if necessary, the other party can continue to send data after the read () returns 0, which directly uses the half-close TCP connection. Muduo will receive the data and notify the Customer Code through message callback.

So when will muduo actually close the socket? In the tcpconnection object structure. Tcpconnection holds a socket object. socket is a raiI handler, and its destructor will close (sockfd _). In this way, if the tcpconnection object leaks, we can find the file descriptor that has not been closed from/proc/Pid/FD/to facilitate error detection.

When muduo returns 0 for read (), it will call back connection callback, And then subtract one from the reference count of tcpconnection. If the reference count of tcpconnection is reduced to zero, it will analyze the structure.

Refer:

Section 18.5 of chapter 1 of TCP/IP explanation, TCP half-close.

The shutdown () function in section 6.6 of the third edition of UNIX network programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.