[][src]Struct tokio::net::TcpSocket

pub struct TcpSocket { /* fields omitted */ }

A TCP socket that has not yet been converted to a TcpStream or TcpListener.

TcpSocket wraps an operating system socket and enables the caller to configure the socket before establishing a TCP connection or accepting inbound connections. The caller is able to set socket option and explicitly bind the socket with a socket address.

The underlying socket is closed when the TcpSocket value is dropped.

TcpSocket should only be used directly if the default configuration used by TcpStream::connect and TcpListener::bind does not meet the required use case.

Calling TcpStream::connect("127.0.0.1:8080") is equivalent to:

use tokio::net::TcpSocket;

use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let addr = "127.0.0.1:8080".parse().unwrap();

    let socket = TcpSocket::new_v4()?;
    let stream = socket.connect(addr).await?;

    Ok(())
}

Calling TcpListener::bind("127.0.0.1:8080") is equivalent to:

use tokio::net::TcpSocket;

use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let addr = "127.0.0.1:8080".parse().unwrap();

    let socket = TcpSocket::new_v4()?;
    // On platforms with Berkeley-derived sockets, this allows to quickly
    // rebind a socket, without needing to wait for the OS to clean up the
    // previous one.
    //
    // On Windows, this allows rebinding sockets which are actively in use,
    // which allows “socket hijacking”, so we explicitly don't set it here.
    // https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
    socket.set_reuseaddr(true)?;
    socket.bind(addr)?;

    let listener = socket.listen(1024)?;

    Ok(())
}

Setting socket options not explicitly provided by TcpSocket may be done by accessing the RawFd/RawSocket using AsRawFd/AsRawSocket and setting the option with a crate like socket2.

Implementations

impl TcpSocket[src]

pub fn new_v4() -> Result<TcpSocket>[src]

This is supported on crate feature net only.

Create a new socket configured for IPv4.

Calls socket(2) with AF_INET and SOCK_STREAM.

Returns

On success, the newly created TcpSocket is returned. If an error is encountered, it is returned instead.

Examples

Create a new IPv4 socket and start listening.

use tokio::net::TcpSocket;

use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let addr = "127.0.0.1:8080".parse().unwrap();
    let socket = TcpSocket::new_v4()?;
    socket.bind(addr)?;

    let listener = socket.listen(128)?;
    Ok(())
}

pub fn new_v6() -> Result<TcpSocket>[src]

This is supported on crate feature net only.

Create a new socket configured for IPv6.

Calls socket(2) with AF_INET6 and SOCK_STREAM.

Returns

On success, the newly created TcpSocket is returned. If an error is encountered, it is returned instead.

Examples

Create a new IPv6 socket and start listening.

use tokio::net::TcpSocket;

use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let addr = "[::1]:8080".parse().unwrap();
    let socket = TcpSocket::new_v6()?;
    socket.bind(addr)?;

    let listener = socket.listen(128)?;
    Ok(())
}

pub fn set_reuseaddr(&self, reuseaddr: bool) -> Result<()>[src]

This is supported on crate feature net only.

Allow the socket to bind to an in-use address.

Behavior is platform specific. Refer to the target platform's documentation for more details.

Examples

use tokio::net::TcpSocket;

use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let addr = "127.0.0.1:8080".parse().unwrap();

    let socket = TcpSocket::new_v4()?;
    socket.set_reuseaddr(true)?;
    socket.bind(addr)?;

    let listener = socket.listen(1024)?;

    Ok(())
}

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

This is supported on crate feature net only.

Bind the socket to the given address.

This calls the bind(2) operating-system function. Behavior is platform specific. Refer to the target platform's documentation for more details.

Examples

Bind a socket before listening.

use tokio::net::TcpSocket;

use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let addr = "127.0.0.1:8080".parse().unwrap();

    let socket = TcpSocket::new_v4()?;
    socket.bind(addr)?;

    let listener = socket.listen(1024)?;

    Ok(())
}

pub async fn connect(self, addr: SocketAddr) -> Result<TcpStream>[src]

This is supported on crate feature net only.

Establish a TCP connection with a peer at the specified socket address.

The TcpSocket is consumed. Once the connection is established, a connected TcpStream is returned. If the connection fails, the encountered error is returned.

This calls the connect(2) operating-system function. Behavior is platform specific. Refer to the target platform's documentation for more details.

Examples

Connecting to a peer.

use tokio::net::TcpSocket;

use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let addr = "127.0.0.1:8080".parse().unwrap();

    let socket = TcpSocket::new_v4()?;
    let stream = socket.connect(addr).await?;

    Ok(())
}

pub fn listen(self, backlog: u32) -> Result<TcpListener>[src]

This is supported on crate feature net only.

Convert the socket into a TcpListener.

backlog defines the maximum number of pending connections are queued by the operating system at any given time. Connection are removed from the queue with TcpListener::accept. When the queue is full, the operationg-system will start rejecting connections.

This calls the listen(2) operating-system function, marking the socket as a passive socket. Behavior is platform specific. Refer to the target platform's documentation for more details.

Examples

Create a TcpListener.

use tokio::net::TcpSocket;

use std::io;

#[tokio::main]
async fn main() -> io::Result<()> {
    let addr = "127.0.0.1:8080".parse().unwrap();

    let socket = TcpSocket::new_v4()?;
    socket.bind(addr)?;

    let listener = socket.listen(1024)?;

    Ok(())
}

Trait Implementations

impl AsRawFd for TcpSocket[src]

impl Debug for TcpSocket[src]

impl FromRawFd for TcpSocket[src]

unsafe fn from_raw_fd(fd: RawFd) -> TcpSocket[src]

Converts a RawFd to a TcpSocket.

Notes

The caller is responsible for ensuring that the socket is in non-blocking mode.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.