Retry with different port

This commit is contained in:
Silvio Braendle 2025-01-27 11:28:36 +01:00
parent a80b935316
commit ff31af2bdc
No known key found for this signature in database
GPG key ID: 553A5A37BA69D8AE
3 changed files with 33 additions and 4 deletions

View file

@ -2,6 +2,12 @@ use crate::protocol::Message;
use crate::sound_scheduler::SoundScheduler; use crate::sound_scheduler::SoundScheduler;
use std::net::UdpSocket; use std::net::UdpSocket;
pub struct BindError {
pub message: String,
}
type Result<T> = std::result::Result<T, BindError>;
pub struct ClientHandler; pub struct ClientHandler;
impl ClientHandler { impl ClientHandler {
@ -13,8 +19,17 @@ impl ClientHandler {
} }
} }
pub fn listen() { pub fn listen(port: u16) -> Result<()> {
let socket = UdpSocket::bind("0.0.0.0:3000").expect("Could not bind to address"); let bind_addr = format!("0.0.0.0:{}", port);
let socket = UdpSocket::bind(bind_addr);
let socket = match socket {
Ok(s) => s,
Err(e) => {
return Err(BindError {
message: e.to_string(),
});
}
};
println!("Listening on {}", socket.local_addr().unwrap()); println!("Listening on {}", socket.local_addr().unwrap());

View file

@ -1,5 +1,5 @@
use std::fs::File; use std::fs::File;
use std::io::{BufReader, Read}; use std::io::BufReader;
use std::net::UdpSocket; use std::net::UdpSocket;
use std::thread; use std::thread;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};

View file

@ -20,7 +20,21 @@ fn main() -> eframe::Result {
}; };
thread::spawn(move || { thread::spawn(move || {
ClientHandler::listen(); let result = ClientHandler::listen(3000);
match result {
Ok(_) => {}
Err(e) => {
eprintln!("Failed to bind: {}", e.message);
// Try with port 3001
let result = ClientHandler::listen(3001);
match result {
Ok(_) => {}
Err(e) => {
eprintln!("Failed to bind again: {}", e.message);
}
}
}
}
}); });
eframe::run_native( eframe::run_native(