I am experimenting with mio to build a high performance tcp server. I started from the example here https://github.com/tokio-rs/mio/blob/master/examples/tcp_server.rs and hit it with hyperfine running this code as a sort of benchmark / feeling the server out.
fn main() {
let mut stream = TcpStream::connect("127.0.0.1:9000").expect("Failed to connect to server.");
stream.write("some_data".as_bytes()).unwrap();
}
This always errors out with the follwing
Error: Os { code: 54, kind: ConnectionReset, message: "Connection reset by peer" }
Thrown from line 156 in the example.
I understand this is the expected behaviour if this error is thrown what I am trying to understand what would cause the TcpStream to throw this error in the first place. I feel this example should handle quite a lot of load.
Also if this is maybe not the best place to start building with Mio I'd appreciate any pointers as to where to look.
The TCP stack will issue a TCP reset if the socket is closed while there are still unread data. This TCP reset will then result in a "Connection reset by peer" (ECONNRESET) error in the peer.
The server you link to writes data and reads data. Your client only writes data, but closes the connection without reading the data from the server. This means there are unread data from the server when the client is closed, causing the client TCP stack to generate a TCP reset and the server to get "Connection reset by peer".