Struct std::net::unix::UnixListener
A Unix domain socket server
After creating a UnixListener
by binding it to a socket address, it listens
for incoming Unix stream connections. These can be accepted by calling accept.
Example
use std::net::{UnixListener, UnixAddr};
use std::fs::Path;
let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap();
let listener = UnixListener::bind(&addr).unwrap();
defer listener.close();
loop {
let (stream, addr) = listener.accept().unwrap();
defer stream.close();
println!("New connection from {}", addr);
// Handle the connection...
}
Run this example
Fields
-
socket: Socket
Methods
impl UnixListener { ... }
-
fn from_socket(socket: Socket) -> UnixListener
Creates a Unix listener socket from a raw socket.
-
fn bind(addr: &UnixAddr) -> Result<UnixListener>
Creates a Unix listener bound to the specified socket address.
The socket will listen for incoming connections with a default backlog.
Example
use std::net::{UnixListener, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let listener = UnixListener::bind(&addr).unwrap(); defer listener.close();
Run this example -
fn accept(self: &UnixListener) -> Result<(UnixStream, UnixAddr)>
Accepts a new incoming connection.
This function will block until a connection is established. Returns the connected stream and the address of the peer.
Example
use std::net::{UnixListener, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let listener = UnixListener::bind(&addr).unwrap(); defer listener.close(); let (stream, peer_addr) = listener.accept().unwrap(); defer stream.close(); println!("Accepted connection from {}", peer_addr);
Run this example -
fn socket_addr(self: &UnixListener) -> Result<SocketAddr>
Returns the local socket address of this listener.
Example
use std::net::{UnixListener, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let listener = UnixListener::bind(&addr).unwrap(); defer listener.close(); let local = listener.socket_addr().unwrap(); println!("Listening on {}", local);
Run this example -
fn set_nonblocking(self: &mut UnixListener, nonblocking: bool) -> Result<()>
Sets the socket to non-blocking or blocking mode.
In non-blocking mode, accept operations will return immediately with an error if they would block.
Example
use std::net::{UnixListener, UnixAddr}; use std::fs::Path; let addr = UnixAddr::from_path(Path::new("/tmp/my.sock")).unwrap(); let listener = UnixListener::bind(&addr).unwrap(); defer listener.close(); listener.set_nonblocking(true).unwrap();
Run this example -
fn close(self: &mut UnixListener) -> Result<()>
Closes the socket.
Note: This does not remove the socket file from the filesystem. You may need to manually delete it.
-
fn as_fd(self: &UnixListener) -> FileDescriptor
-
fn move(self: &mut UnixListener) -> UnixListener