Struct UnixStream

pub struct UnixStream(pub(in ::os::windows::net) Socket);

A Unix stream socket.

Under Windows, it will only work starting from Windows 10 17063.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;
use std::io::prelude::*;

fn main() -> std::io::Result<()> {
    let mut stream = UnixStream::connect("/path/to/my/socket")?;
    stream.write_all(b"hello world")?;
    let mut response = String::new();
    stream.read_to_string(&mut response)?;
    println!("{response}");
    Ok(())
}

Fields

0: Socket

Implementations

impl UnixStream

fn connect<P: AsRef<Path>>(path: P) -> Result<UnixStream>

Connects to the socket named by path.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;

let socket = match UnixStream::connect("/tmp/sock") {
    Ok(sock) => sock,
    Err(e) => {
        println!("Couldn't connect: {e:?}");
        return
    }
};
fn connect_addr(socket_addr: &SocketAddr) -> Result<UnixStream>

Connects to the socket specified by address.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::{UnixListener, UnixStream};

fn main() -> std::io::Result<()> {
    let listener = UnixListener::bind("/path/to/the/socket")?;
    let addr = listener.local_addr()?;

    let sock = match UnixStream::connect_addr(&addr) {
        Ok(sock) => sock,
        Err(e) => {
            println!("Couldn't connect: {e:?}");
            return Err(e)
        }
    };
    Ok(())
}
fn local_addr(&self) -> Result<SocketAddr>

Returns the socket address of the local half of this connection.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    let addr = socket.local_addr().expect("Couldn't get local address");
    Ok(())
}
fn peer_addr(&self) -> Result<SocketAddr>

Returns the socket address of the remote half of this connection.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    let addr = socket.peer_addr().expect("Couldn't get peer address");
    Ok(())
}
fn read_timeout(&self) -> Result<Option<Duration>>

Returns the read timeout of this socket.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;
use std::time::Duration;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
    assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
    Ok(())
}
fn set_nonblocking(&self, nonblocking: bool) -> Result<()>

Moves the socket into or out of nonblocking mode.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    socket.set_nonblocking(true).expect("Couldn't set nonblocking");
    Ok(())
}
fn set_read_timeout(&self, dur: Option<Duration>) -> Result<()>

Sets the read timeout for the socket.

If the provided value is None, then read calls will block indefinitely. An Err is returned if the zero Duration is passed to this method.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;
use std::time::Duration;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
    Ok(())
}

An Err is returned if the zero Duration is passed to this method:

#![feature(windows_unix_domain_sockets)]
use std::io;
use std::os::windows::net::UnixStream;
use std::time::Duration;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
    let err = result.unwrap_err();
    assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
    Ok(())
}
fn set_write_timeout(&self, dur: Option<Duration>) -> Result<()>

Sets the write timeout for the socket.

If the provided value is None, then write calls will block indefinitely. An Err is returned if the zero Duration is passed to this method.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;
use std::time::Duration;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    socket.set_write_timeout(Some(Duration::new(1, 0)))
        .expect("Couldn't set write timeout");
    Ok(())
}

An Err is returned if the zero Duration is passed to this method:

#![feature(windows_unix_domain_sockets)]
use std::io;
use std::os::windows::net::UnixStream;
use std::time::Duration;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
    let err = result.unwrap_err();
    assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
    Ok(())
}
fn shutdown(&self, how: Shutdown) -> Result<()>

Shuts down the read, write, or both halves of this connection.

This function will cause all pending and future I/O calls on the specified portions to immediately return with an appropriate value (see the documentation of Shutdown).

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;
use std::net::Shutdown;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    socket.shutdown(Shutdown::Both).expect("shutdown function failed");
    Ok(())
}
fn take_error(&self) -> Result<Option<Error>>

Returns the value of the SO_ERROR option.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    if let Ok(Some(err)) = socket.take_error() {
        println!("Got error: {err:?}");
    }
    Ok(())
}
fn try_clone(&self) -> Result<UnixStream>

Creates a new independently owned handle to the underlying socket.

The returned UnixStream is a reference to the same stream that this object references. Both handles will read and write the same stream of data, and options set on one stream will be propagated to the other stream.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    let sock_copy = socket.try_clone().expect("Couldn't clone socket");
    Ok(())
}
fn write_timeout(&self) -> Result<Option<Duration>>

Returns the write timeout of this socket.

Examples

#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixStream;
use std::time::Duration;

fn main() -> std::io::Result<()> {
    let socket = UnixStream::connect("/tmp/sock")?;
    socket.set_write_timeout(Some(Duration::new(1, 0)))
        .expect("Couldn't set write timeout");
    assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
    Ok(())
}

Trait Implementations

impl AsRawSocket for UnixStream

fn as_raw_socket(&self) -> RawSocket

impl AsSocket for UnixStream

fn as_socket(&self) -> BorrowedSocket<'_>

impl Debug for UnixStream

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

impl FromRawSocket for UnixStream

unsafe fn from_raw_socket(sock: RawSocket) -> Self

impl IntoRawSocket for UnixStream

fn into_raw_socket(self) -> RawSocket

impl Read for UnixStream

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

impl Write for UnixStream

fn write(&mut self, buf: &[u8]) -> Result<usize>
fn flush(&mut self) -> Result<()>

Auto Trait Implementations

impl Freeze for UnixStream

impl RefUnwindSafe for UnixStream

impl Send for UnixStream

impl Sync for UnixStream

impl Unpin for UnixStream

impl UnsafeUnpin for UnixStream

impl UnwindSafe for UnixStream

Blanket Implementations

impl<R> SpecReadByte for UnixStream where R: Read,

fn spec_read_byte(&mut self) -> Option<Result<u8, Error>>

impl<T> Any for UnixStream where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for UnixStream where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for UnixStream where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for UnixStream

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for UnixStream where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for UnixStream

impl<T, U> Into<U> for UnixStream where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for UnixStream where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for UnixStream where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>