Skip to main content

Socket

Struct Socket 

Source
pub struct Socket { /* private fields */ }
Expand description

An owned, non-blocking socket. Closed on drop.

Implementations§

Source§

impl Socket

Source

pub fn new(family: i32, ty: i32) -> Result<Socket>

Creates a raw non-blocking socket.

Source

pub fn tcp_listen(addr: SocketAddr, opts: &ListenOptions) -> Result<Socket>

A non-blocking TCP listener bound to addr.

Source

pub fn udp_bind(addr: SocketAddr, opts: &UdpOptions) -> Result<Socket>

A non-blocking UDP socket bound to addr.

Source

pub fn tcp_connect(addr: SocketAddr) -> Result<Socket>

Connects a TCP socket to addr, waiting for the handshake, and returns it non-blocking with TCP_NODELAY set. Meant for clients and tests.

Source

pub fn accept(&self) -> Result<(Socket, Option<SocketAddr>)>

Accepts one pending connection without blocking. The new socket is non-blocking.

Source

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

Receives into buf without blocking. Ok(0) means the peer closed its side.

Source

pub fn recv_into(&self, buf: &mut Vec<u8>) -> Result<usize>

Receives into the spare capacity of buf and extends its length by the bytes read.

Source

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

Sends from buf without blocking (never raises SIGPIPE).

Source

pub fn send_vectored(&self, bufs: &[&[u8]]) -> Result<usize>

Sends several buffers with one call (sendmsg / WSASend) without blocking. At most 64 buffers are sent per call.

Source

pub fn shutdown(&self, how: Shutdown) -> Result<()>

Shuts down one or both directions.

Source

pub fn set_nodelay(&self, on: bool) -> Result<()>

Sets or clears TCP_NODELAY.

Source

pub fn nodelay(&self) -> Result<bool>

Whether TCP_NODELAY is set.

Source

pub fn set_nonblocking(&self, on: bool) -> Result<()>

Switches non-blocking mode.

Source

pub fn local_addr(&self) -> Result<SocketAddr>

The local address.

Source

pub fn peer_addr(&self) -> Result<SocketAddr>

The peer address.

Source

pub fn take_error(&self) -> Result<Option<Error>>

Reads and clears the pending socket error (SO_ERROR).

Source

pub fn setsockopt_int(&self, level: i32, name: i32, value: i32) -> Result<()>

Sets an integer socket option.

Source

pub fn getsockopt_int(&self, level: i32, name: i32) -> Result<i32>

Reads an integer socket option.

Source

pub fn raw(&self) -> RawSock

The raw handle (still owned by this Socket).

Source

pub unsafe fn from_raw(raw: RawSock) -> Socket

Takes ownership of a raw handle.

§Safety

raw must be an open socket that nothing else owns or will close.

Source

pub fn into_raw(self) -> RawSock

Gives up ownership of the raw handle.

Source

pub fn into_tcp_stream(self) -> TcpStream ⓘ

Converts into a std TCP stream (it stays non-blocking).

Source

pub fn into_tcp_listener(self) -> TcpListener

Converts into a std TCP listener (it stays non-blocking).

Source

pub fn into_udp_socket(self) -> UdpSocket

Converts into a std UDP socket (it stays non-blocking).

Source

pub fn try_clone(&self) -> Result<Socket>

An independent handle to the same socket (dup / WSADuplicateSocket).

Source§

impl Socket

Source

pub fn recv_msgs( &self, bufs: &mut [Vec<u8>], meta: &mut [RecvMeta], ) -> Result<usize>

Receives up to bufs.len() (at most BATCH) datagrams without blocking. Each buffer is cleared and filled up to its capacity; meta[i] describes bufs[i]. Returns how many were received, or a WouldBlock error when none was waiting.

Source

pub fn send_msgs(&self, msgs: &[Transmit<'_>]) -> Result<usize>

Sends up to BATCH transmits without blocking. Returns how many were handed to the kernel; a WouldBlock error means none was (the socket buffer is full).

Trait Implementations§

Source§

impl Debug for Socket

Source§

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

Formats the value using the given formatter. Read more
Source§

impl From<TcpListener> for Socket

Source§

fn from(s: TcpListener) -> Socket

Takes over a std socket. Call set_nonblocking(true) on it first if it will be driven by an event loop.

Source§

impl From<TcpStream> for Socket

Source§

fn from(s: TcpStream) -> Socket

Takes over a std socket. Call set_nonblocking(true) on it first if it will be driven by an event loop.

Source§

impl From<UdpSocket> for Socket

Source§

fn from(s: UdpSocket) -> Socket

Takes over a std socket. Call set_nonblocking(true) on it first if it will be driven by an event loop.

Auto Trait Implementations§

§

impl Forget for Socket

§

impl Freeze for Socket

§

impl Move for Socket

§

impl RefUnwindSafe for Socket

§

impl Send for Socket

§

impl Sync for Socket

§

impl Unpin for Socket

§

impl UnsafeUnpin for Socket

§

impl UnwindSafe for Socket

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.