pub struct UnixListener(/* private fields */);windows_unix_domain_sockets #150487)Expand description
A structure representing a Unix domain socket server.
§Examples
#![feature(windows_unix_domain_sockets)]
use std::thread;
use std::os::windows::net::{UnixStream, UnixListener};
fn handle_client(stream: UnixStream) {
// ...
}
fn main() -> std::io::Result<()> {
let listener = UnixListener::bind("/path/to/the/socket")?;
// accept connections and process them, spawning a new thread for each one
for stream in listener.incoming() {
match stream {
Ok(stream) => {
/* connection succeeded */
thread::spawn(|| handle_client(stream));
}
Err(err) => {
/* connection failed */
break;
}
}
}
Ok(())
}Implementations§
Source§impl UnixListener
impl UnixListener
Sourcepub fn bind<P: AsRef<Path>>(path: P) -> Result<UnixListener>
🔬This is a nightly-only experimental API. (windows_unix_domain_sockets #150487)
pub fn bind<P: AsRef<Path>>(path: P) -> Result<UnixListener>
windows_unix_domain_sockets #150487)Creates a new UnixListener bound to the specified socket.
§Examples
Sourcepub fn bind_addr(socket_addr: &SocketAddr) -> Result<UnixListener>
🔬This is a nightly-only experimental API. (windows_unix_domain_sockets #150487)
pub fn bind_addr(socket_addr: &SocketAddr) -> Result<UnixListener>
windows_unix_domain_sockets #150487)Creates a new UnixListener bound to the specified socket address.
§Examples
#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::{UnixListener};
fn main() -> std::io::Result<()> {
let listener1 = UnixListener::bind("path/to/socket")?;
let addr = listener1.local_addr()?;
let listener2 = match UnixListener::bind_addr(&addr) {
Ok(sock) => sock,
Err(err) => {
println!("Couldn't bind: {err:?}");
return Err(err);
}
};
Ok(())
}Sourcepub fn accept(&self) -> Result<(UnixStream, SocketAddr)>
🔬This is a nightly-only experimental API. (windows_unix_domain_sockets #150487)
pub fn accept(&self) -> Result<(UnixStream, SocketAddr)>
windows_unix_domain_sockets #150487)Accepts a new incoming connection to this listener.
This function will block the calling thread until a new Unix connection
is established. When established, the corresponding UnixStream and
the remote peer’s address will be returned.
§Examples
#![feature(windows_unix_domain_sockets)]
use std::os::windows::net::UnixListener;
fn main() -> std::io::Result<()> {
let listener = UnixListener::bind("/path/to/the/socket")?;
match listener.accept() {
Ok((socket, addr)) => println!("Got a client: {addr:?}"),
Err(e) => println!("accept function failed: {e:?}"),
}
Ok(())
}Sourcepub fn local_addr(&self) -> Result<SocketAddr>
🔬This is a nightly-only experimental API. (windows_unix_domain_sockets #150487)
pub fn local_addr(&self) -> Result<SocketAddr>
windows_unix_domain_sockets #150487)Returns the local socket address of this listener.
§Examples
Sourcepub fn try_clone(&self) -> Result<UnixListener>
🔬This is a nightly-only experimental API. (windows_unix_domain_sockets #150487)
pub fn try_clone(&self) -> Result<UnixListener>
windows_unix_domain_sockets #150487)Creates a new independently owned handle to the underlying socket.
The returned UnixListener is a reference to the same socket that this
object references. Both handles can be used to accept incoming
connections and options set on one listener will affect the other.
§Examples
Sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
🔬This is a nightly-only experimental API. (windows_unix_domain_sockets #150487)
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
windows_unix_domain_sockets #150487)Moves the socket into or out of nonblocking mode.
This will result in the accept operation becoming nonblocking,
i.e., immediately returning from their calls. If the IO operation is
successful, Ok is returned and no further action is required. If the
IO operation could not be completed and needs to be retried, an error
with kind io::ErrorKind::WouldBlock is returned.
§Examples
Sourcepub fn take_error(&self) -> Result<Option<Error>>
🔬This is a nightly-only experimental API. (windows_unix_domain_sockets #150487)
pub fn take_error(&self) -> Result<Option<Error>>
windows_unix_domain_sockets #150487)Returns the value of the SO_ERROR option.
§Examples
Sourcepub fn incoming(&self) -> Incoming<'_> ⓘ
🔬This is a nightly-only experimental API. (windows_unix_domain_sockets #150487)
pub fn incoming(&self) -> Incoming<'_> ⓘ
windows_unix_domain_sockets #150487)Returns an iterator over incoming connections.
The iterator will never return None and will also not yield the
peer’s SocketAddr structure.
§Examples
#![feature(windows_unix_domain_sockets)]
use std::thread;
use std::os::windows::net::{UnixStream, UnixListener};
fn handle_client(stream: UnixStream) {
// ...
}
fn main() -> std::io::Result<()> {
let listener = UnixListener::bind("/path/to/the/socket")?;
for stream in listener.incoming() {
match stream {
Ok(stream) => {
thread::spawn(|| handle_client(stream));
}
Err(err) => {
break;
}
}
}
Ok(())
}