私はRustにエコーサーバを書き込もうとしています。破損したパイプはいつTCPストリームで発生しますか?
use std::net::{TcpStream, TcpListener};
use std::io::prelude::*;
fn main() {
let listener = TcpListener::bind("0.0.0.0:8000").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
println!("A connection established");
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();
println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
stream.write(&buffer[..]).unwrap();
stream.flush().unwrap();
}
予想通りnc localhost 8000
と最初の要求が作動しているが、後続の要求ではありません。私は間違って何をしていますか?サーバーがクライアントからの要求をどのように読み取っているかに問題がありますか?エラーサーバー側はありませんが。
私は、端末上でそれらを入力してデータを送信しています:あなたはもう一方の端が閉じられたストリームに書き込むとき
$ nc localhost 8000
hi
hi
hello
# no response
# on pressing enter
Ncat: Broken pipe.
"その後のリクエストはありません。" 'nc'経由でデータをどのように送っていますか?正確なエラーは何ですか?あなたはコードが私のために働くようです。 – loganfsmyth
@loganfsmyth 端末に入力してデータを送信しています。質問 – codeNoob