Does TCP close( ) ensure that all the data is delivered to the receiver

801 views Asked by At

I am implementing a simple proxy application in which I always receive data from one end and send to the other end.

In such a case, once I am sure that I am done with receiving all the data from the incoming leg, can I call directly close( ) without calling shutdown( )? If I do so, will close( ) ensure that all the data is delivered to the destination on the outgoing leg and received by the application at the destination?

Or in such a case, is it mandatory that I initiate a shutdown before I initiate a close?

1

There are 1 answers

1
Maxim Egorushkin On

can I call directly close() without calling shutdown()

You can. shutdown is only necessary if you would like to half-close the connection (i.e. read or write end).

will the close( ) ensure that all the data is delivered to the destination on the outgoing leg and received by the application at the destination

close does in the background what a blocking send does, no more, no less. If that background send fails though, you will not get any error indication.

There is also SO_LINGER socket option you can tweak if necessary:

  SO_LINGER
         Sets or gets the SO_LINGER option.  The  argument  is  a  linger
         structure.

             struct linger {
                 int l_onoff;    /* linger active */
                 int l_linger;   /* how many seconds to linger for */
             };

         When  enabled,  a  close(2) or shutdown(2) will not return until
         all queued messages for the socket have been  successfully  sent
         or  the  linger  timeout  has been reached.  Otherwise, the call
         returns immediately and the closing is done in  the  background.
         When  the socket is closed as part of exit(2), it always lingers
         in the background.