Struct axnet::TcpSocket

source ·
pub struct TcpSocket { /* private fields */ }
Expand description

A TCP socket that provides POSIX-like APIs.

  • connect is for TCP clients.
  • bind, listen, and accept are for TCP servers.
  • Other methods are for both TCP clients and servers.

Implementations§

source§

impl TcpSocket

source

pub const fn new() -> Self

Creates a new TCP socket.

source

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

Returns the local address and port, or Err(NotConnected) if not connected.

source

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

Returns the remote address and port, or Err(NotConnected) if not connected.

source

pub fn is_nonblocking(&self) -> bool

Returns whether this socket is in nonblocking mode.

source

pub fn set_nonblocking(&self, nonblocking: bool)

Moves this TCP stream into or out of nonblocking mode.

This will result in read, write, recv and send operations becoming nonblocking, i.e., immediately returning from their calls. If the IO operation is successful, Ok is returned and no further action is required. If the IO operation could not be completed and needs to be retried, an error with kind Err(WouldBlock) is returned.

source

pub fn connect(&self, remote_addr: SocketAddr) -> AxResult

Connects to the given address and port.

The local port is generated automatically.

source

pub fn bind(&self, local_addr: SocketAddr) -> AxResult

Binds an unbound socket to the given address and port.

If the given port is 0, it generates one automatically.

It’s must be called before listen and accept.

source

pub fn listen(&self) -> AxResult

Starts listening on the bound address and port.

It’s must be called after bind and before accept.

source

pub fn accept(&self) -> AxResult<TcpSocket>

Accepts a new connection.

This function will block the calling thread until a new TCP connection is established. When established, a new TcpSocket is returned.

It’s must be called after bind and listen.

source

pub fn shutdown(&self) -> AxResult

Close the connection.

source

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

Receives data from the socket, stores it in the given buffer.

source

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

Transmits data in the given buffer.

source

pub fn poll(&self) -> AxResult<PollState>

Whether the socket is readable or writable.

Trait Implementations§

source§

impl Drop for TcpSocket

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Sync for TcpSocket

Auto Trait Implementations§

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>,

§

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>,

§

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.