pub trait TcpSocket {
// Required methods
async fn connect(
&mut self,
address: SocketAddr,
) -> Result<impl TcpConnection, Error>;
async fn accept(
&mut self,
address: SocketAddr,
) -> Result<(impl TcpConnection, SocketAddr), Error>;
}
Expand description
TCP socket for establishing connections.
A socket can only handle one connection at a time.
For multiple connections, use multiple socket instances.
While no socket is actively listening, incoming connections are rejected via RST
packet.
This differs from the typical behavior of TCP sockets on Linux.
§Example
use veecle_osal_api::net::tcp::{TcpSocket, TcpConnection};
use core::net::SocketAddr;
async fn connect_example(mut socket: impl TcpSocket)
{
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
let connection = socket.connect(addr).await.unwrap();
// Use connection for reading/writing.
connection.close().await;
}
Required Methods§
Sourceasync fn connect(
&mut self,
address: SocketAddr,
) -> Result<impl TcpConnection, Error>
async fn connect( &mut self, address: SocketAddr, ) -> Result<impl TcpConnection, Error>
Connects to a remote TCP server.
Sourceasync fn accept(
&mut self,
address: SocketAddr,
) -> Result<(impl TcpConnection, SocketAddr), Error>
async fn accept( &mut self, address: SocketAddr, ) -> Result<(impl TcpConnection, SocketAddr), Error>
Accepts an incoming TCP connection.
Binds to the specified address and waits for an incoming connection. Returns the connection and the remote peer’s address.
Listens on all devices if an all-zero IP is provided.
Does not support using the 0
port to listen on an automatically assigned port.
§Loopback
Depending on the platform, the remote peer’s address might be set to 127.0.0.1
,
regardless of the actual IPv4 used.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.