You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am not sure if this my own bug; expected behaviour; or something can be improved in the tutorial.
When I run the client code reproduced below, the process hangs. I would've expected it to exit after receiving all the payload.
use tokio::io::{self,AsyncReadExt,AsyncWriteExt};use tokio::net::TcpStream;#[tokio::main]asyncfnmain() -> io::Result<()>{let socket = TcpStream::connect("127.0.0.1:6142").await?;let(mut rd,mut wr) = io::split(socket);// Write data in the background
tokio::spawn(asyncmove{
wr.write_all(b"hello\r\n").await?;
wr.write_all(b"world\r\n").await?;// Sometimes, the rust type inferencer needs// a little helpOk::<_, io::Error>(())});letmut buf = vec![0; 128];loop{let n = rd.read(&mut buf).await?;if n == 0{break;}println!("GOT {:?}", &buf[..n]);}Ok(())}
I would've naively thought that when the writing task above returns, that will close the socket for writing but still keep it open for reading. At least on linux, this can be accomplished by using shutdown. This will cause the echo server to get an EOF when it tries to read from the socket and cause it to close the socket which will then cause the client to see n == 0 condition when it calls reads; break; and then the process would exit.
Maybe this is not io::split works. The smallest change to make might be to add a note to the tutorial to indicate that the client does not exit. A more involved change might be see how to get the client to properly exit.
The text was updated successfully, but these errors were encountered:
Hi I am a newbie of Tokio and also stuck in this issue. shutdown indeed avoid hangs, but what I am trying to do is an echo client that can repeatedly send and receive bytes, the pseudo code is like:
read a line from stdin
send it to the echo server
receive the eco and print it to stdout
go back to 1
I need to reuse the writer so I can't use shutdown , what should I do?
I am not sure if this my own bug; expected behaviour; or something can be improved in the tutorial.
When I run the client code reproduced below, the process hangs. I would've expected it to exit after receiving all the payload.
Output:
I would've naively thought that when the writing task above returns, that will close the socket for writing but still keep it open for reading. At least on linux, this can be accomplished by using shutdown. This will cause the echo server to get an
EOF
when it tries to read from the socket and cause it to close the socket which will then cause the client to seen == 0
condition when it calls reads; break; and then the process would exit.Maybe this is not
io::split
works. The smallest change to make might be to add a note to the tutorial to indicate that the client does not exit. A more involved change might be see how to get the client to properly exit.The text was updated successfully, but these errors were encountered: