Retry with different port
This commit is contained in:
parent
a80b935316
commit
ff31af2bdc
3 changed files with 33 additions and 4 deletions
|
|
@ -2,6 +2,12 @@ use crate::protocol::Message;
|
|||
use crate::sound_scheduler::SoundScheduler;
|
||||
use std::net::UdpSocket;
|
||||
|
||||
pub struct BindError {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, BindError>;
|
||||
|
||||
pub struct ClientHandler;
|
||||
|
||||
impl ClientHandler {
|
||||
|
|
@ -13,8 +19,17 @@ impl ClientHandler {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn listen() {
|
||||
let socket = UdpSocket::bind("0.0.0.0:3000").expect("Could not bind to address");
|
||||
pub fn listen(port: u16) -> Result<()> {
|
||||
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());
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
use std::io::BufReader;
|
||||
use std::net::UdpSocket;
|
||||
use std::thread;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
|
|
|||
16
src/main.rs
16
src/main.rs
|
|
@ -20,7 +20,21 @@ fn main() -> eframe::Result {
|
|||
};
|
||||
|
||||
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(
|
||||
|
|
|
|||
Loading…
Reference in a new issue