Add defaults for UDP arguments to not get in the way.

This commit is contained in:
mattbk
2025-10-04 22:42:14 -05:00
parent 8065fcc24e
commit 6101932b0d

View File

@@ -9,6 +9,7 @@ use std::net::TcpStream;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
use std::net::UdpSocket; use std::net::UdpSocket;
use std::net::Ipv4Addr;
/// Validate that the provided port string can be parsed into a u16 and is nonzero. /// Validate that the provided port string can be parsed into a u16 and is nonzero.
fn validate_port(port: &str) -> Result<u16, String> { fn validate_port(port: &str) -> Result<u16, String> {
@@ -49,16 +50,12 @@ struct Cli {
#[arg(short = 'u', long, default_value_t = false)] #[arg(short = 'u', long, default_value_t = false)]
ui_only: bool, ui_only: bool,
// Enable UDP
#[arg(short = 'm', long, default_value_t = false)]
udp: bool,
// Send UDP to what IP address (e.g. 127.0.0.1) // Send UDP to what IP address (e.g. 127.0.0.1)
#[arg(short = 'b', long)] #[arg(short = 'b', long, default_value_t = std::net::IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)))]
uip: std::net::IpAddr, uip: std::net::IpAddr,
// Send UDP to what port (e.g. 8000) // Send UDP to what port (e.g. 8000)
#[arg(short = 'k', long, value_parser = validate_port)] #[arg(short = 'k', long, value_parser = validate_port, default_value_t = 55555)]
uport: u16, uport: u16,
} }
@@ -426,10 +423,10 @@ fn main() -> Result<()> {
let uaddr = format!("{}:{}", cli.uip, cli.uport); let uaddr = format!("{}:{}", cli.uip, cli.uport);
let reconnect_delay_ms = 5000; let reconnect_delay_ms = 5000;
if cli.udp { if cli.uport != 55555 {
// Bind the client socket to any available address and port // Bind the client socket to any available address and port
let socket = UdpSocket::bind("0.0.0.0:0")?; let socket = UdpSocket::bind("0.0.0.0:0")?;
println!("UDP client started"); println!("UDP client started at {}:{}", cli.uip, cli.uport);
// Send a message to the server // Send a message to the server
let message = "UDP client connected"; let message = "UDP client connected";
socket.send_to(message.as_bytes(), uaddr)?; socket.send_to(message.as_bytes(), uaddr)?;