Struct Socket

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

A POSIX socket.

Implementations§

Source§

impl<Address: AsSocketAddress> Socket<Address>

Source

pub fn new(kind: c_int, protocol: c_int) -> Result<Self>
where Address: SpecificSocketAddress,

Create a new socket with the specified type and protocol.

The domain is taken from the Address type.

The created socket has the close-on-exec flag set. The flag will be set atomically when the socket is created if the platform supports it.

See man socket for more information.

Source

pub fn new_generic(domain: c_int, kind: c_int, protocol: c_int) -> Result<Self>

Create a new socket with the specified domain, type and protocol.

Unless you are working with generic socket addresses, you should normally prefer Self::new.

The created socket has the close-on-exec flag set. The flag will be set atomically when the socket is created if the platform supports it.

See man socket for more information.

Source

pub fn pair(kind: c_int, protocol: c_int) -> Result<(Self, Self)>
where Address: SpecificSocketAddress,

Create a connected pair of socket with the specified type and protocol.

The domain is taken from the Address type.

The created sockets have the close-on-exec flag set. The flag will be set atomically when the sockets are created if the platform supports it.

See man socketpair and man socket for more information.

Source

pub fn pair_generic( domain: c_int, kind: c_int, protocol: c_int, ) -> Result<(Self, Self)>

Create a connected pair of socket with the specified domain, type and protocol.

Unless you are working with generic socket addresses, you should normally prefer Self::pair.

The created sockets have the close-on-exec flag set. The flag will be set atomically when the sockets are created if the platform supports it.

See man socketpair and man socket for more information.

Source

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

Try to clone the socket.

This is implemented by duplicating the file descriptor. The returned Socket refers to the same kernel object.

The underlying file descriptor of the new socket will have the close-on-exec flag set. If the platform supports it, the flag will be set atomically when the file descriptor is duplicated.

Source

pub unsafe fn from_raw_fd(fd: RawFd) -> Self

Wrap a raw file descriptor in a Socket.

This function sets no flags or options on the file descriptor or socket. It is your own responsibility to make sure the close-on-exec flag is already set, and that the SO_NOSIGPIPE option is set on Apple platforms.

Source

pub fn as_raw_fd(&self) -> RawFd

Get the raw file descriptor.

This function does not release ownership of the underlying file descriptor. The file descriptor will still be closed when the FileDesc is dropped.

Source

pub fn into_raw_fd(self) -> RawFd

Release and get the raw file descriptor.

This function releases ownership of the underlying file descriptor. The file descriptor will not be closed.

Source

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

Put the socket in blocking or non-blocking mode.

Source

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

Check if the socket in blocking or non-blocking mode.

Source

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

Gets the value of the SO_ERROR option on this socket.

This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.

Source

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

Get the local address the socket is bound to.

Source

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

Get the remote address the socket is connected to.

Source

pub fn connect(&self, address: &Address) -> Result<()>

Connect the socket to a remote address.

It depends on the exact socket type what it means to connect the socket. See man connect for more information.

Source

pub fn bind(&self, address: &Address) -> Result<()>

Bind the socket to a local address.

It depends on the exact socket type what it means to bind the socket. See man bind for more information.

Source

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

Put the socket in listening mode, ready to accept connections.

Once the socket is in listening mode, new connections can be accepted with accept().

Not all socket types can be put into listening mode. See man listen for more information.

Source

pub fn accept(&self) -> Result<(Self, Address)>

Accept a new connection on the socket.

The socket must have been put in listening mode with a call to listen().

Not all socket types can be put into listening mode or accept connections. See man listen for more information.

Source

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

Send data over the socket to the connected peer.

Returns the number of transferred bytes, or an error.

See man send for more information.

Source

pub fn send_to( &self, data: &[u8], address: &Address, flags: c_int, ) -> Result<usize>

Send data over the socket to the specified address.

This function is only valid for connectionless protocols such as UDP or unix datagram sockets.

Returns the number of transferred bytes, or an error.

See man sendto for more information.

Source

pub fn send_msg( &self, data: &[IoSlice<'_>], cdata: Option<&[u8]>, flags: c_int, ) -> Result<usize>

Send a message over the socket to the connected peer.

Returns the number of transferred bytes, or an error.

See man sendmsg for more information.

Source

pub fn send_msg_to( &self, address: &Address, data: &[IoSlice<'_>], cdata: Option<&[u8]>, flags: c_int, ) -> Result<usize>

Send a message over the socket to the specified address.

This function is only valid for connectionless protocols such as UDP or unix datagram sockets.

Returns the number of transferred bytes, or an error.

See man sendmsg for more information.

Source

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

Receive a data on the socket from the connected peer.

Returns the number of transferred bytes, or an error.

See man recv for more information.

Source

pub fn recv_from( &self, buffer: &mut [u8], flags: c_int, ) -> Result<(Address, usize)>

Receive a data on the socket.

Returns the address of the sender and the number of transferred bytes, or an error.

See man recvfrom for more information.

Source

pub fn recv_msg( &self, data: &[IoSliceMut<'_>], cdata: &mut SocketAncillary<'_>, flags: c_int, ) -> Result<(usize, c_int)>

Receive a message on the socket from the connected peer.

If the call succeeds, the function returns a tuple with:

  • the number of transferred bytes
  • the number of transferred control message bytes
  • the reception flags

See man recvmsg for more information.

Source

pub fn recv_msg_from( &self, data: &[IoSliceMut<'_>], cdata: &mut SocketAncillary<'_>, flags: c_int, ) -> Result<(Address, usize, c_int)>

Receive a message on the socket from any address.

If the call succeeds, the function returns a tuple with:

  • the address of the sender
  • the number of transferred bytes
  • the number of transferred control message bytes
  • the reception flags

See man recvmsg for more information.

Trait Implementations§

Source§

impl<Address: AsSocketAddress> AsRawFd for &Socket<Address>

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl<Address: AsSocketAddress> AsRawFd for Socket<Address>

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl<Address: AsSocketAddress> FromRawFd for Socket<Address>

Source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more
Source§

impl<Address: AsSocketAddress> IntoRawFd for Socket<Address>

Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

§

impl<Address> Freeze for Socket<Address>

§

impl<Address> RefUnwindSafe for Socket<Address>

§

impl<Address> Send for Socket<Address>

§

impl<Address> Sync for Socket<Address>

§

impl<Address> Unpin for Socket<Address>

§

impl<Address> UnwindSafe for Socket<Address>

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.