pub struct Socket { /* private fields */ }Expand description
An owned, non-blocking socket. Closed on drop.
Implementations§
Source§impl Socket
impl Socket
Sourcepub fn tcp_listen(addr: SocketAddr, opts: &ListenOptions) -> Result<Socket>
pub fn tcp_listen(addr: SocketAddr, opts: &ListenOptions) -> Result<Socket>
A non-blocking TCP listener bound to addr.
Sourcepub fn udp_bind(addr: SocketAddr, opts: &UdpOptions) -> Result<Socket>
pub fn udp_bind(addr: SocketAddr, opts: &UdpOptions) -> Result<Socket>
A non-blocking UDP socket bound to addr.
Sourcepub fn tcp_connect(addr: SocketAddr) -> Result<Socket>
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.
Sourcepub fn accept(&self) -> Result<(Socket, Option<SocketAddr>)>
pub fn accept(&self) -> Result<(Socket, Option<SocketAddr>)>
Accepts one pending connection without blocking. The new socket is non-blocking.
Sourcepub fn recv(&self, buf: &mut [u8]) -> Result<usize>
pub fn recv(&self, buf: &mut [u8]) -> Result<usize>
Receives into buf without blocking. Ok(0) means the peer closed its side.
Sourcepub fn recv_into(&self, buf: &mut Vec<u8>) -> Result<usize>
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.
Sourcepub fn send(&self, buf: &[u8]) -> Result<usize>
pub fn send(&self, buf: &[u8]) -> Result<usize>
Sends from buf without blocking (never raises SIGPIPE).
Sourcepub fn send_vectored(&self, bufs: &[&[u8]]) -> Result<usize>
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.
Sourcepub fn set_nodelay(&self, on: bool) -> Result<()>
pub fn set_nodelay(&self, on: bool) -> Result<()>
Sets or clears TCP_NODELAY.
Sourcepub fn set_nonblocking(&self, on: bool) -> Result<()>
pub fn set_nonblocking(&self, on: bool) -> Result<()>
Switches non-blocking mode.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
The local address.
Sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
The peer address.
Sourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
Reads and clears the pending socket error (SO_ERROR).
Sourcepub fn setsockopt_int(&self, level: i32, name: i32, value: i32) -> Result<()>
pub fn setsockopt_int(&self, level: i32, name: i32, value: i32) -> Result<()>
Sets an integer socket option.
Sourcepub fn getsockopt_int(&self, level: i32, name: i32) -> Result<i32>
pub fn getsockopt_int(&self, level: i32, name: i32) -> Result<i32>
Reads an integer socket option.
Sourcepub unsafe fn from_raw(raw: RawSock) -> Socket
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.
Sourcepub fn into_tcp_stream(self) -> TcpStream ⓘ
pub fn into_tcp_stream(self) -> TcpStream ⓘ
Converts into a std TCP stream (it stays non-blocking).
Sourcepub fn into_tcp_listener(self) -> TcpListener
pub fn into_tcp_listener(self) -> TcpListener
Converts into a std TCP listener (it stays non-blocking).
Sourcepub fn into_udp_socket(self) -> UdpSocket
pub fn into_udp_socket(self) -> UdpSocket
Converts into a std UDP socket (it stays non-blocking).
Source§impl Socket
impl Socket
Sourcepub fn recv_msgs(
&self,
bufs: &mut [Vec<u8>],
meta: &mut [RecvMeta],
) -> Result<usize>
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.
Trait Implementations§
Source§impl From<TcpListener> for Socket
impl From<TcpListener> for Socket
Source§fn from(s: TcpListener) -> Socket
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.