-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pedromiguelmiranda/server
added rust server
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "chat-server" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::io::{Read, Write}; | ||
use std::net::{TcpListener, TcpStream}; | ||
use std::sync::{Arc, Mutex}; | ||
use std::thread; | ||
|
||
fn handle_client( mut stream: TcpStream, clients: Arc<Mutex<Vec<TcpStream>>>) { | ||
let client_stream = stream.try_clone().expect("Failed to clone stream."); | ||
|
||
{ | ||
println!("Client connected: {}", client_stream.peer_addr().unwrap()); | ||
let mut clients = clients.lock().unwrap(); | ||
clients.push(client_stream.try_clone().expect("Failed to clone stream.")); | ||
} | ||
|
||
let mut buf = [0; 512]; | ||
loop { | ||
match stream.read(&mut buf) { | ||
Ok(0) => { | ||
// let mut clients = clients.lock().unwrap(); | ||
// clients.retain(|c| *c != stream); | ||
println!("Client disconnected: {}", stream.peer_addr().unwrap()); | ||
return; | ||
} | ||
Ok(n) => { | ||
let msg = String::from_utf8_lossy(&buf[..n]); | ||
println!("Received message: {} from {}", msg, stream.peer_addr().unwrap()); | ||
broadcast(&clients, msg); | ||
} | ||
Err(e) => { | ||
eprintln!("Error reading from socket: {}", e); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn broadcast(clients: &Arc<Mutex<Vec<TcpStream>>>, msg: std::borrow::Cow<'_, str>) { | ||
|
||
let clients = clients.lock().unwrap(); | ||
|
||
for mut client in clients.iter() { | ||
let _ = client.write(msg.as_bytes()); | ||
} | ||
} | ||
|
||
fn main() { | ||
let listener = TcpListener::bind("127.0.0.1:8080").expect("Failed to bind"); | ||
println!("Server listening on port 8080..."); | ||
|
||
let clients: Arc<Mutex<Vec<TcpStream>>> = Arc::new(Mutex::new(Vec::new())); | ||
|
||
for stream in listener.incoming() { | ||
match stream { | ||
Ok(stream) => { | ||
let clients = Arc::clone(&clients); | ||
thread::spawn(move || { | ||
handle_client(stream, clients); | ||
}); | ||
} | ||
Err(e) => { | ||
eprintln!("Error accepting connection: {}", e); | ||
} | ||
} | ||
} | ||
} |