pub struct Socket { /* private fields */ }
Expand description
Raw socket
Implementations§
Source§impl Socket
impl Socket
Sourcepub fn new(family: c_int, _type: c_int, protocol: c_int) -> Result<Socket>
pub fn new(family: c_int, _type: c_int, protocol: c_int) -> Result<Socket>
Initializes new socket.
Corresponds to C connect()
Sourcepub fn raw(&self) -> SOCKET
pub fn raw(&self) -> SOCKET
Returns underlying socket descriptor.
Note: ownership is not transferred.
Sourcepub fn name(&self) -> Result<SocketAddr>
pub fn name(&self) -> Result<SocketAddr>
Retrieves socket name i.e. address
Wraps getsockname()
Available for binded/connected sockets.
Sourcepub fn bind(&self, addr: &SocketAddr) -> Result<()>
pub fn bind(&self, addr: &SocketAddr) -> Result<()>
Binds socket to address.
Sourcepub fn listen(&self, backlog: c_int) -> Result<()>
pub fn listen(&self, backlog: c_int) -> Result<()>
Listens for incoming connections on this socket.
Sourcepub fn recv(&self, buf: &mut [u8], flags: c_int) -> Result<usize>
pub fn recv(&self, buf: &mut [u8], flags: c_int) -> Result<usize>
Receives some bytes from socket
Number of received bytes is returned on success
Sourcepub fn recv_from(
&self,
buf: &mut [u8],
flags: c_int,
) -> Result<(usize, SocketAddr)>
pub fn recv_from( &self, buf: &mut [u8], flags: c_int, ) -> Result<(usize, SocketAddr)>
Receives some bytes from socket
Number of received bytes and remote address are returned on success.
Sourcepub fn send(&self, buf: &[u8], flags: c_int) -> Result<usize>
pub fn send(&self, buf: &[u8], flags: c_int) -> Result<usize>
Sends some bytes through socket.
Number of sent bytes is returned.
Sourcepub fn send_to(
&self,
buf: &[u8],
peer_addr: &SocketAddr,
flags: c_int,
) -> Result<usize>
pub fn send_to( &self, buf: &[u8], peer_addr: &SocketAddr, flags: c_int, ) -> Result<usize>
Sends some bytes through socket toward specified peer.
Number of sent bytes is returned.
Note: the socket will be bound, if it isn’t already.
Use method name
to determine address.
Sourcepub fn accept4(&self, flags: AcceptFlags) -> Result<(Socket, SocketAddr)>
pub fn accept4(&self, flags: AcceptFlags) -> Result<(Socket, SocketAddr)>
Accept a new incoming client connection and return its files descriptor and address.
This is an emulation of the corresponding Unix system call, that will automatically call
.set_blocking
and .set_inheritable
with parameter values based on the value of flags
on the created client socket:
AcceptFlags::NON_BLOCKING
– Mark the newly created socket as non-blockingAcceptFlags::NON_INHERITABLE
– Mark the newly created socket as not inheritable by client processes
Sourcepub fn accept(&self) -> Result<(Socket, SocketAddr)>
pub fn accept(&self) -> Result<(Socket, SocketAddr)>
Accepts incoming connection.
Sourcepub fn connect(&self, addr: &SocketAddr) -> Result<()>
pub fn connect(&self, addr: &SocketAddr) -> Result<()>
Connects socket with remote address.
Sourcepub fn set_opt<T>(&self, level: c_int, name: c_int, value: T) -> Result<()>
pub fn set_opt<T>(&self, level: c_int, name: c_int, value: T) -> Result<()>
Sets socket option
Value is generally integer or C struct.
Sourcepub fn ioctl(&self, request: c_int, value: c_ulong) -> Result<()>
pub fn ioctl(&self, request: c_int, value: c_ulong) -> Result<()>
Sets I/O parameters of socket.
It uses ioctlsocket
under hood.
Sourcepub fn set_blocking(&self, value: bool) -> Result<()>
pub fn set_blocking(&self, value: bool) -> Result<()>
Sets non-blocking mode.
Sourcepub fn set_inheritable(&self, value: bool) -> Result<()>
pub fn set_inheritable(&self, value: bool) -> Result<()>
Sets whether this socket will be inherited by child processes or not.
Internally this implemented by calling SetHandleInformation(sock, HANDLE_FLAG_INHERIT, …)
.
Sourcepub fn get_inheritable(&self) -> Result<bool>
pub fn get_inheritable(&self) -> Result<bool>
Returns whether this socket will be inherited by child processes or not.
Sourcepub fn shutdown(&self, direction: ShutdownType) -> Result<()>
pub fn shutdown(&self, direction: ShutdownType) -> Result<()>
Stops receive and/or send over socket.