Small updates, little improvements

parent 9f57f74b
......@@ -4,3 +4,13 @@
*.lock
Lucien-Rust/target/
\.idea/
cmake-build-debug/
Lucien/Rust/target/
Lucien/Rust/Rust\.iml
Lucien/server/
use std::net::TcpStream;
use std::io::{Read, Write};
use std::io::{stdin, stdout};
fn get_entry() -> String {
let mut buf = String::new();
stdin().read_line(&mut buf).unwrap();
buf.replace("\n", "").replace("\r", "")
}
fn exchange_with_server(mut stream: TcpStream) {
let stdout = stdout();
let mut io = stdout.lock();
let buf = &mut [0; 1024];
println!("Enter `quit` when you want to leave");
loop {
write!(io, "> ").unwrap();
io.flush().unwrap();
match &*get_entry() {
"quit" => {
println!("bye!");
return;
}
"exit" => {
println!("bye!");
return;
}
line => {
write!(stream, "{}\n", line).unwrap();
match stream.read(buf) {
Ok(received) => {
if received < 1 {
println!("Perte de la connexion avec le serveur");
return;
}
}
Err(_) => {
println!("Perte de la connexion avec le serveur");
return;
}
}
// println!("Réponse du serveur : {}", buf);
let reponse = String::from_utf8(buf.to_vec()).unwrap();
println!("Réponse du serveur : {}", reponse);
}
}
}
}
pub fn client(server_address: String) {
println!("Tentative de connexion a serveur...");
match TcpStream::connect(server_address) {
Ok(stream) => {
println!("Connexion au serveur réussie !");
exchange_with_server(stream);
}
Err(e) => {
println!("La connection au serveur a échoué : {}", e);
}
}
}
use std::env;
pub mod client;
pub mod server;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() == 2 {
///////////////////////////////////////////////////////////////////////
// Server opened //
///////////////////////////////////////////////////////////////////////
println!("Opening server on port {}", args[1]);
// serveur(args[1].clone());
server::serveur(args[1].clone());
} else if args.len() == 3 {
///////////////////////////////////////////////////////////////////////
// Client opened //
///////////////////////////////////////////////////////////////////////
println!("Client connecting on server {}:{}", args[1], args[2]);
let mut serv = args[1].clone();
serv.push(':');
serv.push_str(&args[2]);
client::client(serv);
} else {
println!("Usage: {} [server ip] port", args[0]);
}
}
use std::env;
use std::net::{TcpListener, TcpStream};
use std::io::{Read, Write};
use std::thread;
#[allow(unused_imports)]
use std::io::{stdin, stdout, Read, Write};
#[allow(unused_imports)]
use std::sync::{Arc, Mutex};
///////////////////////////////////////////////////////////////////////////////
// //
// Client //
// //
///////////////////////////////////////////////////////////////////////////////
fn get_entry() -> String {
let mut buf = String::new();
stdin().read_line(&mut buf).unwrap();
buf.replace("\n", "").replace("\r", "")
}
fn exchange_with_server(mut stream: TcpStream) {
let stdout = std::io::stdout();
let mut io = stdout.lock();
let buf = &mut [0; 3];
println!("Enter `quit` when you want to leave");
loop {
write!(io, "> ").unwrap();
io.flush().unwrap();
match &*get_entry() {
"quit" => {
println!("bye!");
return;
}
"exit" => {
println!("bye!");
return;
}
line => {
write!(stream, "{}\n", line).unwrap();
match stream.read(buf) {
Ok(received) => {
if received < 1 {
println!("Perte de la connexion avec le serveur");
return;
}
}
Err(_) => {
println!("Perte de la connexion avec le serveur");
return;
}
}
// println!("Réponse du serveur : {:?}", buf);
let reponse = String::from_utf8(buf.to_vec()).unwrap();
println!("Réponse du serveur : {}", reponse);
}
}
}
}
fn client(server_address: String) {
println!("Tentative de connexion a serveur...");
match TcpStream::connect(server_address) {
Ok(stream) => {
println!("Connexion au serveur réussie !");
exchange_with_server(stream);
}
Err(e) => {
println!("La connection au serveur a échoué : {}", e);
}
}
}
///////////////////////////////////////////////////////////////////////////////
// //
// Server //
// //
///////////////////////////////////////////////////////////////////////////////
fn handle_client(mut stream: &TcpStream, adresse: &str) {
fn handle_client(mut stream: &TcpStream, adresse: &str, name: String) {
let mut msg: Vec<u8> = Vec::new();
loop {
let buf = &mut [0; 10];
......@@ -100,7 +24,8 @@ fn handle_client(mut stream: &TcpStream, adresse: &str) {
x += 1;
if *c == '\n' as u8 {
println!(
"message reçu {} : {}",
"message reçu {}({}) : {}",
name,
adresse,
// on convertit maintenant notre buffer en String
String::from_utf8(msg).unwrap()
......@@ -122,7 +47,7 @@ fn handle_client(mut stream: &TcpStream, adresse: &str) {
}
}
fn serveur(port: String) {
pub fn serveur(port: String) {
println!("Port: {}", port);
let mut serv = String::from("127.0.0.1:");
serv.push_str(&port);
......@@ -132,15 +57,17 @@ fn serveur(port: String) {
// Multi-client ///////////////////////////////////////////////////////////
for stream in listener.incoming() {
match stream {
match stream {
Ok(stream) => {
let adresse = match stream.peer_addr() {
Ok(addr) => format!("[adresse : {}]", addr),
Err(_) => "inconnue".to_owned(),
};
let name = String::from("Toto");
println!("Nouveau client {}", adresse);
thread::spawn(move || handle_client(&stream, &*adresse));
thread::spawn(move || handle_client(&stream, &*adresse, name));
}
Err(e) => {
println!("La connexion du client a échoué : {}", e);
......@@ -149,29 +76,3 @@ fn serveur(port: String) {
println!("En attente d’un autre client...");
}
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() == 2 {
///////////////////////////////////////////////////////////////////////
// Server opened //
///////////////////////////////////////////////////////////////////////
println!("Opening server on port {}", args[1]);
serveur(args[1].clone());
} else if args.len() == 3 {
///////////////////////////////////////////////////////////////////////
// Client opened //
///////////////////////////////////////////////////////////////////////
println!("Client connecting on server {}:{}", args[1], args[2]);
let mut serv = if args[1] == String::from("localhost") {
String::from("127.0.0.1")
} else {
args[1].clone()
};
serv.push(':');
serv.push_str(&args[2]);
client(serv);
} else {
println!("Usage: {} [server ip] port", args[0]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment