10

So on linux, shutdown() can take a parameter SHUT_RD, SHUT_WR or SHUT_RDWR to shutdown only part of the communication channel. But in terms of the TCP messages sending to the peer, how does it work?

In TCP state machine, the closing works in a 4-way handshake fashion,

(1)           (2)
FIN---------->

   <----------ACK
   <----------FIN

ACK----------->

So what messages dose it send when I do a shutdown(sock, SHUT_RD) or shutdown(sock, SHUT_WR)?

3 Answers 3

18
  1. shutdown(sd, SHUT_WR) sends a FIN which the peer responds to with an ACK. Any further attempts to write to the socket will incur an error. However the peer can still continue to send data.

  2. shutdown(sd, SHUT_RD) sends nothing on the network: it just conditions the local API to return EOS for any subsequent reads on the socket. The behaviour when receiving data on a socket that has been shutdown for read is system-dependent: Unix will ACK it and throw it away; Linux will ACK it and buffer it, which will eventually stall the sender; Windows will issue an RST, which the sender sees as 'connection reset by peer'.

1

The FIN packets don't have to be symmetric. Each end sends FIN when its local writer has closed the socket.

5
  • What if Host1 does a SHUT_RD? Nothing will be sent right? What happens if the peer Host2 sends a message? Does HOST1 reply a RST, or it just ignores it?
    – wei
    Feb 6, 2013 at 23:45
  • I believe it should send a RST, yes. But I'm too lazy to test. Note that there's nothing you can do from the read end to force the other host on the network not to send any packets, so I suspect TCP implementations are likely to differ on this...
    – Andy Ross
    Feb 7, 2013 at 0:47
  • yeah, exactly. So if HOST1 replies a RST, then wouldn't HOST2 close its socket because HOST2 doesn't know what's going on with HOST1 and will treat this RST a regular RST? Then if HOST2 closes the socket, HOST1's WRITE half that's left wouldn't work anymore, would it?
    – wei
    Feb 7, 2013 at 1:03
  • 4
    @wei The behaviour when receiving data on a socket that has been shutdown for read is system-dependent: Unix will ACK it and throw it away; Linux will ACK it and buffer it, which will eventually stall the sender; Windows will issue an RST, which the sender sees as 'connection reset by peer'.
    – user207421
    Feb 7, 2013 at 2:25
  • @EJP, thanks for clear it up for me. However, I am not sure how to accept a comment as an answer.
    – wei
    Feb 7, 2013 at 2:33
0

shutdown with write end send FIN and close will close the socket .so any attempt to send close socket will result RESET packet.

I met with intresting problem where sender send: Packet A send Pakcet B send shutdown with write FIN shutdown read close socket

but recevier receive out of order Packet A Pakcket containing FIN Packet B ACK from reciver ,this cause connection reset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.