pub struct UnixListener(_);
Expand description
A structure representing a Unix domain socket server.
Examples
use std::thread;
use std::os::unix::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
sourceimpl UnixListener
impl UnixListener
sourcepub fn bind<P>(path: P) -> Result<UnixListener, Error> where
P: AsRef<Path>,
pub fn bind<P>(path: P) -> Result<UnixListener, Error> where
P: AsRef<Path>,
Creates a new UnixListener
bound to the specified socket.
Examples
use std::os::unix::net::UnixListener;
let listener = match UnixListener::bind("/path/to/the/socket") {
Ok(sock) => sock,
Err(e) => {
println!("Couldn't connect: {e:?}");
return
}
};
sourcepub fn bind_addr(socket_addr: &SocketAddr) -> Result<UnixListener, Error>
🔬 This is a nightly-only experimental API. (unix_socket_abstract
)
pub fn bind_addr(socket_addr: &SocketAddr) -> Result<UnixListener, Error>
unix_socket_abstract
)Creates a new UnixListener
bound to the specified socket address
.
Examples
#![feature(unix_socket_abstract)]
use std::os::unix::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), Error>
pub fn accept(&self) -> Result<(UnixStream, SocketAddr), Error>
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
use std::os::unix::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 try_clone(&self) -> Result<UnixListener, Error>
pub fn try_clone(&self) -> Result<UnixListener, Error>
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
use std::os::unix::net::UnixListener;
fn main() -> std::io::Result<()> {
let listener = UnixListener::bind("/path/to/the/socket")?;
let listener_copy = listener.try_clone().expect("try_clone failed");
Ok(())
}
sourcepub fn local_addr(&self) -> Result<SocketAddr, Error>
pub fn local_addr(&self) -> Result<SocketAddr, Error>
Returns the local socket address of this listener.
Examples
use std::os::unix::net::UnixListener;
fn main() -> std::io::Result<()> {
let listener = UnixListener::bind("/path/to/the/socket")?;
let addr = listener.local_addr().expect("Couldn't get local address");
Ok(())
}
sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
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
use std::os::unix::net::UnixListener;
fn main() -> std::io::Result<()> {
let listener = UnixListener::bind("/path/to/the/socket")?;
listener.set_nonblocking(true).expect("Couldn't set non blocking");
Ok(())
}
sourcepub fn take_error(&self) -> Result<Option<Error>, Error>
pub fn take_error(&self) -> Result<Option<Error>, Error>
Returns the value of the SO_ERROR
option.
Examples
use std::os::unix::net::UnixListener;
fn main() -> std::io::Result<()> {
let listener = UnixListener::bind("/tmp/sock")?;
if let Ok(Some(err)) = listener.take_error() {
println!("Got error: {err:?}");
}
Ok(())
}
Platform specific
On Redox this always returns None
.
sourcepub fn incoming(&self) -> Incoming<'_>ⓘNotable traits for Incoming<'a>impl<'a> Iterator for Incoming<'a> type Item = Result<UnixStream, Error>;
pub fn incoming(&self) -> Incoming<'_>ⓘNotable traits for Incoming<'a>impl<'a> Iterator for Incoming<'a> type Item = Result<UnixStream, Error>;
Returns an iterator over incoming connections.
The iterator will never return None
and will also not yield the
peer’s SocketAddr
structure.
Examples
use std::thread;
use std::os::unix::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(())
}
Trait Implementations
1.63.0 · sourceimpl AsFd for UnixListener
impl AsFd for UnixListener
sourcefn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Borrows the file descriptor. Read more
sourceimpl AsRawFd for UnixListener
impl AsRawFd for UnixListener
sourceimpl Debug for UnixListener
impl Debug for UnixListener
1.63.0 · sourceimpl From<OwnedFd> for UnixListener
impl From<OwnedFd> for UnixListener
sourcefn from(fd: OwnedFd) -> UnixListener
fn from(fd: OwnedFd) -> UnixListener
Converts to this type from the input type.
1.63.0 · sourceimpl From<UnixListener> for OwnedFd
impl From<UnixListener> for OwnedFd
sourcefn from(listener: UnixListener) -> OwnedFd
fn from(listener: UnixListener) -> OwnedFd
Converts to this type from the input type.
sourceimpl FromRawFd for UnixListener
impl FromRawFd for UnixListener
sourceunsafe fn from_raw_fd(fd: i32) -> UnixListener
unsafe fn from_raw_fd(fd: i32) -> UnixListener
Constructs a new instance of Self
from the given raw file
descriptor. Read more
sourceimpl<'a> IntoIterator for &'a UnixListener
impl<'a> IntoIterator for &'a UnixListener
type Item = Result<UnixStream, Error>
type Item = Result<UnixStream, Error>
The type of the elements being iterated over.
sourceimpl IntoRawFd for UnixListener
impl IntoRawFd for UnixListener
sourcefn into_raw_fd(self) -> i32
fn into_raw_fd(self) -> i32
Consumes this object, returning the raw underlying file descriptor. Read more
sourceimpl UnixListenerExt for UnixListener
impl UnixListenerExt for UnixListener
type Conn = UnixStream
type Conn = UnixStream
The type represeting the stream connection returned by accept_unix_addr()
.
sourcefn bind_unix_addr(on: &UnixSocketAddr) -> Result<UnixListener, Error>
fn bind_unix_addr(on: &UnixSocketAddr) -> Result<UnixListener, Error>
Creates a socket bound to a UnixSocketAddr
and starts listening on it.
sourcefn accept_unix_addr(
&self
) -> Result<(<UnixListener as UnixListenerExt>::Conn, UnixSocketAddr), Error>
fn accept_unix_addr(
&self
) -> Result<(<UnixListener as UnixListenerExt>::Conn, UnixSocketAddr), Error>
Accepts a connection and returns the client’s address as
an uds::UnixSocketAddr
. Read more
sourcefn local_unix_addr(&self) -> Result<UnixSocketAddr, Error>
fn local_unix_addr(&self) -> Result<UnixSocketAddr, Error>
Returns the address this socket is listening on.
Auto Trait Implementations
impl RefUnwindSafe for UnixListener
impl Send for UnixListener
impl Sync for UnixListener
impl Unpin for UnixListener
impl UnwindSafe for UnixListener
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
sourcefn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<W, Global>impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<W, Global>impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
sourcefn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
sourcefn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
sourcefn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
sourceimpl<T> DowncastSync for T where
T: Any + Send + Sync,
impl<T> DowncastSync for T where
T: Any + Send + Sync,
sourceimpl<A> DynCastExt for A
impl<A> DynCastExt for A
sourcefn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
A: DynCastExtHelper<T>,
T: ?Sized,
fn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
A: DynCastExtHelper<T>,
T: ?Sized,
Use this to cast from one trait object type to another. Read more
sourcefn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
T: ?Sized,
fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
T: ?Sized,
Use this to upcast a trait to one of its supertraits. Read more
sourcefn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
T: ?Sized,
fn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
T: ?Sized,
sourcefn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
fn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more