TcpStream read() method


#1

Hi all,

When TcpStream read() method returns 0, it’s not very clear according to the docs. I designed a Rust simple Tcp server and when I close the socket from a Python client, it seems the read() method returns 0.

But it’s not really clear why.

Any clue?


#2

If you pass a non-zero length buffer to read into, a read of 0 indicates end of stream (EOS) - the remote has shut down (at least) its sending half of the duplex.


#3

So in other words: its the regular behavior? When the Python client closed the socket, the read() returns 0 ?


#4

Yup - if the remote closes its end gracefully you’ll get a read of 0; if it does it ungracefully (eg sends a RST packet) then you’ll get an Error variant of the Result.


#5

@vitalyd thanks a lot for the clarification !