Skip to content

Instantly share code, notes, and snippets.

@Geal
Last active March 7, 2022 21:54
Show Gist options
  • Save Geal/8378309 to your computer and use it in GitHub Desktop.
Save Geal/8378309 to your computer and use it in GitHub Desktop.
Rust and Unix domain sockets usage example * stream sockets (client.rs and server.rs) * datagram sockets (datagram_client.rs and datagram_server.rs) * example of writing to syslog (incomplete for now)
extern mod native;
use std::io::net::unix::UnixStream;
use std::path::posix::Path;
use std::str;
use std::rt::rtio::IoFactory;
use native::io;
fn main() {
let server = ~"./hello";
let mut factory = io::IoFactory::new();
println!("got factory");
match factory.unix_connect(&server.to_c_str()) {
Err(e) => println!("factory error: {}", e.desc),
Ok(mut pipe) => {
println!("got pipe");
let mut bytes = [0, .. 1024];
pipe.read(bytes);
println!("got message: {}", str::from_utf8(bytes));
}
}
}
extern mod native;
use std::io::net::unix::UnixStream;
use std::path::posix::Path;
use std::str;
use std::rt::rtio::{IoFactory, RtioDatagramPipe};
use native::io;
fn main() {
let server = ~"./socket";
let client = ~"./socket_client";
let mut factory = io::IoFactory::new();
println!("got factory");
match io::net::UnixStream::dgram_bind(&client.to_c_str()).map(|s| ~s as ~RtioDatagramPipe) {
Err(e) => println!("dgram_bind error: {}", e.desc),
Ok(mut stream) => {
let msg = ~"hello from client";
match stream.sendto(msg.into_bytes(), &server.to_c_str()) {
Err(e) => println!("sendto error: {}", e.desc),
Ok(()) => {
println!("message successfully sent");
let mut buf = [0, ..1024];
match stream.recvfrom(buf) {
Err(e) => println!("recvfrom error: {}", e.desc),
Ok((i,addr)) => {
match addr.as_str() {
None => println!("cannot decode address"),
Some(s) => println!("received message from:{}", s)
}
println!("message: {}", str::from_utf8(buf))
}
}
}
}
}
}
}
extern mod native;
use std::io::net::unix::{UnixStream, UnixListener, UnixAcceptor};
use std::path::posix::Path;
use std::str;
use std::io::{Acceptor, Listener, Writer};
use std::c_str::CString;
use std::rt::rtio::{IoFactory, RtioDatagramPipe};
use native::io;
fn main() {
let server = ~"./pouet";
println!("got path: {}", server);
let mut factory = io::IoFactory::new();
println!("got factory");
match io::net::UnixStream::dgram_bind(&server.to_c_str()).map(|s| ~s as ~RtioDatagramPipe) {
Err(e) => println!("factory error: {}", e.detail),
Ok(mut stream) => {
let mut buf = [0, ..1024];
match stream.recvfrom(buf) {
Err(e) => println!("recvfrom error: {}", e.desc),
Ok((i,addr)) => {
match addr.as_str() {
None => println!("cannot decode address"),
Some(s) => println!("received message from {}", s)
}
println!("message: {}", str::from_utf8(buf));
let msg = ~"hello from server";
let client = ~"./pouet_client";
match stream.sendto(msg.into_bytes(), &client.to_c_str()) {
Err(e) => println!("sendto error: {}", e.desc),
Ok(()) => println!("message successfully sent")
}
}
}
}
}
}
extern mod native;
use std::io::net::unix::{UnixStream, UnixListener, UnixAcceptor};
use std::path::posix::Path;
use std::str;
use std::io::{Acceptor, Listener, Writer};
use std::c_str::CString;
use std::rt::rtio::IoFactory;
use native::io;
fn main() {
let server = ~"./hello";
println!("got path: {}", server);
let mut factory = io::IoFactory::new();
println!("got factory");
match factory.unix_bind(&server.to_c_str()) {
Err(e) => println!("factory error: {}", e.detail),
Ok(listener) => {
println!("listening");
match listener.listen() {
Err(e) => println!("listener error: {}", e.desc),
Ok(mut acceptor) => {
­ println!("accepting");
­ match acceptor.accept() {
­ Err(e) => println!("acceptor error: {}", e.desc),
­ Ok(mut pipe) => {
­ println!("accepted");
let s = ~"hello";
pipe.write(s.into_bytes());
println!("good bye");
­ }
­ }
­}
­}
}
}
}
extern mod native;
use std::io::net::unix::UnixStream;
use std::path::posix::Path;
use std::str;
use std::rt::rtio::{IoFactory, RtioDatagramPipe};
use native::io;
fn main() {
let server = ~"/dev/log";
let client = ~"./syslog_client";
let mut factory = io::IoFactory::new();
println!("got factory");
match io::net::UnixStream::dgram_bind(&client.to_c_str()).map(|s| ~s as ~RtioDatagramPipe) {
Err(e) => println!("dgram_bind error: {}", e.desc),
Ok(mut stream) => {
let msg = ~"ABHello from syslog client";
let mut bytes = msg.into_bytes();
bytes[0] = 1; // USER
bytes[1] = 1; // INFO
match stream.sendto(bytes, &server.to_c_str()) {
Err(e) => println!("sendto error: {}", e.desc),
Ok(()) => {
println!("message successfully sent");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment