Struct Socket

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

Raw socket

Implementations§

Source§

impl Socket

Source

pub fn new(family: c_int, _type: c_int, protocol: c_int) -> Result<Socket>

Initializes new socket.

Corresponds to C connect()

Source

pub fn raw(&self) -> SOCKET

Returns underlying socket descriptor.

Note: ownership is not transferred.

Source

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

Retrieves socket name i.e. address

Wraps getsockname()

Available for binded/connected sockets.

Source

pub fn bind(&self, addr: &SocketAddr) -> Result<()>

Binds socket to address.

Source

pub fn listen(&self, backlog: c_int) -> Result<()>

Listens for incoming connections on this socket.

Source

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

Source

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.

Source

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

Sends some bytes through socket.

Number of sent bytes is returned.

Source

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.

Source

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-blocking
  • AcceptFlags::NON_INHERITABLE – Mark the newly created socket as not inheritable by client processes
Source

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

Accepts incoming connection.

Source

pub fn connect(&self, addr: &SocketAddr) -> Result<()>

Connects socket with remote address.

Source

pub fn get_opt<T>(&self, level: c_int, name: c_int) -> Result<T>

Retrieves socket option.

Source

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.

Source

pub fn ioctl(&self, request: c_int, value: c_ulong) -> Result<()>

Sets I/O parameters of socket.

It uses ioctlsocket under hood.

Source

pub fn set_blocking(&self, value: bool) -> Result<()>

Sets non-blocking mode.

Source

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, …).

Source

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

Returns whether this socket will be inherited by child processes or not.

Source

pub fn shutdown(&self, direction: ShutdownType) -> Result<()>

Stops receive and/or send over socket.

Source

pub fn close(&self) -> Result<()>

Closes socket.

Note: on Drop socket will be closed on its own. There is no need to close it explicitly.

Trait Implementations§

Source§

impl AsRawSocket for Socket

Source§

fn as_raw_socket(&self) -> RawSocket

Extracts the raw socket. Read more
Source§

impl Drop for Socket

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl FromRawSocket for Socket

Source§

unsafe fn from_raw_socket(sock: RawSocket) -> Self

Constructs a new I/O object from the specified raw socket. Read more
Source§

impl IntoRawSocket for Socket

Source§

fn into_raw_socket(self) -> RawSocket

Consumes this object, returning the raw underlying socket. Read more

Auto Trait Implementations§

§

impl Freeze for Socket

§

impl RefUnwindSafe for Socket

§

impl Send for Socket

§

impl Sync for Socket

§

impl Unpin 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.