Reputation: 115
I am writing a socket server in Rust, and have implemented response/send code. The main problem is that the method read_to_string
works until the connection is closed, not until a newline is presented.
For example, in this code,
use std::net::{TcpListener, TcpStream};
use std::io::Write;
use std::io::Read;
fn handle_client(stream: TcpStream) {
let conn = stream;
let conn = send(conn, "Hello!");
let mut conn = send(conn, "Bye!");
fn send(mut stream: TcpStream, s: &str) -> TcpStream {
let giver = format!("{}\n", s);
let _ = stream.write(giver.into_bytes().as_slice());
return stream;
}
let mut response: String = "".to_string();
let length = conn.read_to_string(&mut response); // ignore here too
println!("Message:\n{}\n\nLength: {}", response, length.unwrap());
}
The server will wait for a disconnect before presenting any data.
How can I make it so that the response will parse on newline?
Upvotes: 1
Views: 1956
Reputation: 431549
You want to read a single line, the method for which is called BufRead::read_line
. You can wrap something implementing Read
in a BufReader
:
use std::io::{Write, BufRead, BufReader};
use std::net::TcpStream;
fn handle_client(mut conn: TcpStream) {
writeln!(&mut conn, "Hello!").expect("Unable to write");
writeln!(&mut conn, "Bye!").expect("Unable to write");
let mut response = String::new();
let mut conn = BufReader::new(conn);
let length = conn.read_line(&mut response).expect("unable to read");
println!("Message:\n{}\n\nLength: {}", response, length);
}
fn main() {}
Additional notes on the original code:
Don't ignore errors. If you want to ignore errors, then Rust may not be a language you enjoy using. Errors are designed to be hard to ignore.
There's no reason to re-bind stream
to conn
, just make it mutable.
Use the write!
and writeln!
macros to simplify formatting to something that implements Read
.
Don't use "".to_string
, just use String::new
.
There's no reason to list the type for response
twice (: String
, = String::new()
), just let type inference handle it.
Explicit return
statements are not idiomatic Rust.
Upvotes: 3