I'm writing an server that .read() and .write() the u8 string. Server also stores a variable - A=1.
server get a value of variable B, and than
do this : A = A * B. (Server must save new value!)
and than server sholud to reply to client in u8 string form, should return a new value of A.
When I connect the client to server, it writes:
New connection: 127.0.0.1:1639
On the client terminal I can also see:
Please, write the value of B:
And than, when I write number/numbers, server writes me the error:
thread '' panicked at 'called
Result::unwrap()
on anErr
value: ParseIntError { kind: InvalidDigit }', main.rs:14:41
So, what should I do to fix it?
My server:
fn handle_client(mut stream: TcpStream) {
let mut data = [0 as u8; 30]; // using 30 byte buffer
let mut a: i32 = 1;
while match stream.read(&mut data) {
Ok(size) => {
// конвертируем полученную строку из u8 в String
let temp = str::from_utf8(&data).unwrap();
// конвертируем String в i32
let temp: i32 = temp.parse().unwrap();
// изменяем значение а
a = a * temp;
// теперь i32 конвертируем в String
let st = a.to_string();
// String в u8
let data = st.as_bytes();
// и отдаём эту инфу клиенту
stream.write(&data[0..size]).unwrap();
true
},
Err(_) => {
println!("An error occurred, terminating connection with {}", stream.peer_addr().unwrap());
stream.shutdown(Shutdown::Both).unwrap();
false
}
} {}
}
fn main() {
let listener = TcpListener::bind("0.0.0.0:3333").unwrap();
// accept connections and process them, spawning a new thread for each one
println!("Server listening on port 3333");
println!("A = 1");
for stream in listener.incoming() {
match stream {
Ok(stream) => {
println!("New connection: {}", stream.peer_addr().unwrap());
thread::spawn(move|| {
// connection succeeded
handle_client(stream)
});
}
Err(e) => {
println!("Error: {}", e);
/* connection failed */
}
}
}
// close the socket server
drop(listener);
}