Struct std::net::UnixStream
A Unix domain stream socket
Unix domain sockets provide bidirectional communication between processes on the same machine using filesystem paths as addresses.
Example
use std::net::{UnixStream, UnixAddr};
use std::fs::Path;
let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap();
let stream = UnixStream::connect(&addr).unwrap();
defer stream.close();
stream.write_all("Hello!").unwrap();
Run this example
Fields
-
socket: Socket
Methods
impl UnixStream { ... }
-
fn from_socket(socket: Socket) -> UnixStream
Creates a Unix stream socket from a raw socket.
-
fn connect(addr: &UnixAddr) -> Result<UnixStream>
Connects to a Unix socket at the specified address.
Example
use std::net::{UnixStream, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let stream = UnixStream::connect(&addr).unwrap(); defer stream.close();
Run this example -
fn pair() -> Result<(UnixStream, UnixStream)>
Creates a pair of connected Unix stream sockets.
This is similar to the
socketpair
system call. The two sockets are already connected to each other and can communicate bidirectionally.Example
use std::net::UnixStream; let (sock1, sock2) = UnixStream::pair().unwrap(); defer sock1.close(); defer sock2.close(); sock1.write_all("Hello from sock1!").unwrap(); let buf: [u8; 128]; let n = sock2.read(&buf).unwrap(); assert_eq!(buf[..n] as &[u8], "Hello from sock1!");
Run this example -
fn socket_addr(self: &UnixStream) -> Result<SocketAddr>
Returns the socket address of the local half of this connection.
Example
use std::net::{UnixStream, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let stream = UnixStream::connect(&addr).unwrap(); defer stream.close(); let local = stream.socket_addr().unwrap(); println!("Local address: {}", local);
Run this example -
fn peer_addr(self: &UnixStream) -> Result<SocketAddr>
Returns the socket address of the remote half of this connection.
Example
use std::net::{UnixStream, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let stream = UnixStream::connect(&addr).unwrap(); defer stream.close(); let peer = stream.peer_addr().unwrap(); println!("Peer address: {}", peer);
Run this example -
fn shutdown(self: &UnixStream, how: Shutdown) -> Result<()>
Shuts down the reading or writing end of the socket or both.
Example
use std::net::{UnixStream, UnixAddr, Shutdown}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let stream = UnixStream::connect(&addr).unwrap(); defer stream.close(); stream.write_all("Hello!").unwrap(); stream.shutdown(Shutdown::Write).unwrap();
Run this example -
fn set_read_timeout(self: &UnixStream, duration: Option<Duration>) -> Result<()>
Sets the read timeout for the socket.
If the provided duration is
Option::none()
, then read operations will block indefinitely. Otherwise, read operations will time out after the specified duration.Example
use std::net::{UnixStream, UnixAddr}; use std::fs::Path; use std::time::Duration; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let stream = UnixStream::connect(&addr).unwrap(); defer stream.close(); stream.set_read_timeout(Option::some(Duration::from_secs(5))).unwrap();
Run this example -
fn read_timeout(self: &UnixStream) -> Result<Option<Duration>>
Gets the read timeout for the socket.
Returns
Option::none()
if reads will block indefinitely.Example
use std::net::{UnixStream, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let stream = UnixStream::connect(&addr).unwrap(); defer stream.close(); let timeout = stream.read_timeout().unwrap(); if timeout.is_some() { println!("Read timeout: {} seconds", timeout.unwrap().total_secs()); }
Run this example -
fn set_write_timeout(self: &UnixStream, duration: Option<Duration>) -> Result<()>
Sets the write timeout for the socket.
If the provided duration is
Option::none()
, then write operations will block indefinitely. Otherwise, write operations will time out after the specified duration.Example
use std::net::{UnixStream, UnixAddr}; use std::fs::Path; use std::time::Duration; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let stream = UnixStream::connect(&addr).unwrap(); defer stream.close(); stream.set_write_timeout(Option::some(Duration::from_secs(5))).unwrap();
Run this example -
fn write_timeout(self: &UnixStream) -> Result<Option<Duration>>
Gets the write timeout for the socket.
Returns
Option::none()
if writes will block indefinitely.Example
use std::net::{UnixStream, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let stream = UnixStream::connect(&addr).unwrap(); defer stream.close(); let timeout = stream.write_timeout().unwrap(); if timeout.is_some() { println!("Write timeout: {} seconds", timeout.unwrap().total_secs()); }
Run this example -
fn set_nonblocking(self: &mut UnixStream, nonblocking: bool) -> Result<()>
Sets the socket to non-blocking or blocking mode.
In non-blocking mode, read and write operations will return immediately with an error if they would block.
Example
use std::net::{UnixStream, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let stream = UnixStream::connect(&addr).unwrap(); defer stream.close(); stream.set_nonblocking(true).unwrap();
Run this example -
fn close(self: &mut UnixStream) -> Result<()>
Closes the socket.
-
fn as_fd(self: &UnixStream) -> FileDescriptor
-
fn read(self: &mut UnixStream, buf: &mut [u8]) -> Result<usize>
Reads from the stream into a provided buffer.
If successful, returns the number of bytes read. This may be less than the size of the provided buffer.
-
fn write(self: &mut UnixStream, buf: &[u8]) -> Result<usize>
Write the buffer to the stream.
Returns the number of bytes written. This may be less than the size of the buffer. See also write_all, which can be used to write the entire buffer.
-
fn flush(self: &mut UnixStream) -> Result<()>
Flush the stream
This may be a no-op, depending on the stream.
-
fn move(self: &mut UnixStream) -> UnixStream
Mixins
impl UnixStream { ... }
-
mixin FdReadWrite<UnixStream>
-
mixin Readable<UnixStream>
-
Fills the provided buffer by reading from the stream.
If stream had less bytes available than the size of the buffer, this method will return an
Error::eof()
. -
Reads the entire stream into a
StringBuf
.If successful, returns the number of bytes that were read.
-
mixin Writable<UnixStream>
-
Writes all the bytes in the buffer to the stream.
If EOF is reached, returns
Error::eof()
.