Client finally sends and displays properly messages

This commit is contained in:
Phuntsok Drak-pa 2018-03-21 12:12:35 +01:00
parent d085789fab
commit b9a13e29dd

View File

@ -3,7 +3,7 @@ use std::io::*;
use std::net::TcpStream; use std::net::TcpStream;
use std::thread; use std::thread;
static leave_msg: &str = "BYE"; // static leave_msg: &str = "BYE";
fn get_entry() -> String { fn get_entry() -> String {
let mut buf = String::new(); let mut buf = String::new();
@ -15,23 +15,20 @@ fn get_entry() -> String {
fn write_to_server(stream: TcpStream) { fn write_to_server(stream: TcpStream) {
let mut writer = BufWriter::new(&stream); let mut writer = BufWriter::new(&stream);
macro_rules! send {
($line:expr) => ({
try!(writeln!(writer, "{}", $line));
try!(writer.flush());
})
}
loop { loop {
match &*get_entry() { match &*get_entry() {
"/quit" => { "/quit" => {
println!("Disconnecting..."); println!("Disconnecting...");
send!("BYE"); // send!("BYE");
writeln!(writer, "BYE").unwrap();
writer.flush().unwrap();
println!("Disconnected!"); println!("Disconnected!");
return (); return ();
} }
line => { line => {
send!(line); // send!(line);
writeln!(writer, "{}", line).unwrap();
writer.flush().unwrap();
} }
} }
} }
@ -41,8 +38,9 @@ fn exchange_with_server(stream: TcpStream) {
let server = stream.peer_addr().unwrap(); let server = stream.peer_addr().unwrap();
println!("Connected to {}", server); println!("Connected to {}", server);
// Buffered reading and writing // Buffered reading and writing
let mut reader = BufReader::new(&stream); let stream_cpy = stream.try_clone().unwrap();
let mut writer = BufWriter::new(&stream); let mut reader = BufReader::new(&stream_cpy);
// let mut writer = BufWriter::new(&stream);
println!("Enter `/quit` when you want to leave"); println!("Enter `/quit` when you want to leave");
@ -77,39 +75,12 @@ fn exchange_with_server(stream: TcpStream) {
Ok(_) => { Ok(_) => {
println!("Left?"); println!("Left?");
} }
Err(e) => { Err(_) => {
println!("Disappeared? {}", e); println!(">>> Successfully left the room <<<");
} }
} }
} }
// fn exchange_with_server(stream: TcpStream) {
// let (chan, recv): (Sender<String>, Receiver<String>) = mpsc::channel();
// // let buf = &mut [0; 1024];
// spawn(move || {
// loop {
// let msg = recv.recv().unwrap();
// println!("{}", msg);
// }
// });
// println!("Enter `quit` or `exit` when you want to leave");
// loop {
// match &*get_entry() {
// "quit" => {
// println!("bye!");
// return;
// }
// "exit" => {
// println!("bye!");
// return;
// }
// line => {
// chan.send(format!("{}", line)).unwrap();
// }
// }
// }
// }
pub fn client(server_address: String) { pub fn client(server_address: String) {
println!("Tentative de connexion a serveur..."); println!("Tentative de connexion a serveur...");
match TcpStream::connect(server_address) { match TcpStream::connect(server_address) {