1
0
Fork 0

Add sample server + client unix socket

This commit is contained in:
Florian RICHER 2023-01-24 14:17:32 +01:00
parent b94c621ab1
commit 6d1582792c
No known key found for this signature in database
GPG key ID: 6BF27BF8A1E71623
8 changed files with 127 additions and 27 deletions

41
src/client/main.rs Normal file
View file

@ -0,0 +1,41 @@
use std::os::unix::net::{UnixListener, UnixStream};
use std::io::{Read, Write};
use anyhow::Context;
fn main() -> anyhow::Result<()> {
let socket_path = "mysocket";
let mut unix_stream =
UnixStream::connect(socket_path).context("Could not create stream")?;
write_request_and_shutdown(&mut unix_stream)?;
read_from_stream(&mut unix_stream)?;
Ok(())
}
fn write_request_and_shutdown(unix_stream: &mut UnixStream) -> anyhow::Result<()> {
unix_stream
.write(b"Hello?")
.context("Failed at writing onto the unix stream")?;
println!("We sent a request");
println!("Shutting down writing on the stream, waiting for response...");
unix_stream
.shutdown(std::net::Shutdown::Write)
.context("Could not shutdown writing on the stream")?;
Ok(())
}
fn read_from_stream(unix_stream: &mut UnixStream) -> anyhow::Result<()> {
let mut response = String::new();
unix_stream
.read_to_string(&mut response)
.context("Failed at reading the unix stream")?;
println!("We received this response: {}", response);
Ok(())
}