From 59eec6133079ab7d92c95685e14ddf8052737ac0 Mon Sep 17 00:00:00 2001 From: Robin Lu Date: Wed, 11 Oct 2023 12:43:04 +0800 Subject: [PATCH] main.rs: add show local ip --- Cargo.toml | 4 ++-- src/main.rs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2cc4d54..4479bf8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,8 +2,8 @@ name = "breedenter-rust" version = "0.1.5" edition = "2021" -authors = ["wwng "] +authors = ["wwng "] description = "A rust version of breedenter." [dependencies] -open = "3.2.0" +open = "3.2.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2ceed0c..4cc4590 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,26 @@ use std::io::Read; use std::net::UdpSocket; use std::thread; use std::time::Duration; + +fn localip_get() -> Option { + let socket = match UdpSocket::bind("0.0.0.0:0") { + Ok(s) => s, + Err(_) => return None, + }; + + match socket.connect("192.168.1.1:80") { + Ok(()) => (), + Err(_) => return None, + }; + + match socket.local_addr() { + Ok(addr) => return Some(addr.ip().to_string()), + Err(_) => return None, + }; +} + fn main() -> std::io::Result<()> { + println!("Local IP: {}", localip_get().unwrap()); let socket = UdpSocket::bind("0.0.0.0:37540")?; socket.set_broadcast(true)?; socket.set_nonblocking(true).unwrap();