Created
May 7, 2020 15:09
-
-
Save majecty/9514ca0b84b2a9c8eb8ca67de0b079ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::net::Shutdown; | |
use std::os::unix::net::UnixDatagram; | |
use std::{sync::Arc, thread}; | |
use Shutdown::Both; | |
fn main() -> std::io::Result<()> { | |
let sock = Arc::new(UnixDatagram::bind("./my-unix-socket")?); | |
let mut buf = vec![0; 10]; | |
let builder = thread::Builder::new() | |
.name("foo".into()) | |
.stack_size(32 * 1024); | |
let sock2 = sock.clone(); | |
builder | |
.spawn(move || { | |
std::thread::sleep(std::time::Duration::from_secs(3)); | |
// macOS에선 shutdown을 호출하는 대신 데이타그램 소켓에 직접 exit을 보낸다. | |
// sock2.shutdown(Both).unwrap(); | |
let send_socket = UnixDatagram::unbound().unwrap(); | |
send_socket.connect("./my-unix-socket").unwrap(); | |
send_socket.send(b"exit").unwrap(); | |
}) | |
.unwrap(); | |
loop { | |
let read_len = sock.recv(buf.as_mut_slice()).unwrap(); | |
if read_len == 0 { | |
break; | |
} | |
println!("Received: {:?}", &buf[0..read_len]); | |
if read_len == 4 && &buf[0..read_len] == b"exit" { | |
break; | |
} | |
} | |
Ok(()) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment