Skip to content

Commit

Permalink
main.rs: add show local ip
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Lu committed Oct 11, 2023
1 parent b754188 commit 59eec61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
name = "breedenter-rust"
version = "0.1.5"
edition = "2021"
authors = ["wwng <[email protected]>"]
authors = ["wwng <[email protected]>"]
description = "A rust version of breedenter."

[dependencies]
open = "3.2.0"
open = "3.2.0"
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,26 @@ use std::io::Read;
use std::net::UdpSocket;
use std::thread;
use std::time::Duration;

fn localip_get() -> Option<String> {
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();
Expand Down

0 comments on commit 59eec61

Please sign in to comment.