Why my message doesn't write into the socket when I try to read the response after sending it?

53 Views Asked by At
let mut stream = TcpStream::connect("192.168.56.1:24301").unwrap();

let input = String::from("LISTMON");

let mut buffer = [0; 128];

// Send message to server
stream.write(input.as_bytes()).expect("Erreur lors de l'envoi du message");
stream.flush().expect("Erreur lors du vidage du tampon");

// Read response from server
let n = stream.read(&mut buffer[..]).expect("Erreur lors de la lecture de la réponse");
println!("{:?}", &buffer[..n]);`

Here is the code that I made, it sends the message "LISTMON" to my server coded in Java (it works perfect, I already did a client in Java and everything works) and my server is supposed to response with a list of available protocols.

The code above doesn't write my message to my server but if I remove the line to read the response it works, am pretty lost. It seems like my program needs to shut down for the message to be sent.

let mut stream = TcpStream::connect("192.168.56.1:24301").unwrap();

let input = String::from("LISTMON");

let mut buffer = [0; 128];

// Send message to server
stream.write(input.as_bytes()).expect("Erreur lors de l'envoi du message");
stream.flush().expect("Erreur lors du vidage du tampon");

The code above as said writes the message perfectly but I need to read the response as you guess so what am I doing wrong?

0

There are 0 best solutions below