Struct mio::net::TcpSocket[][src]

pub struct TcpSocket { /* fields omitted */ }
This is supported on crate feature net only.
Expand description

A non-blocking TCP socket used to configure a stream or listener.

The TcpSocket type wraps the operating-system’s socket handle. This type is used to configure the socket before establishing a connection or start listening for inbound connections.

The socket will be closed when the value is dropped.

Implementations

Create a new IPv4 TCP socket.

This calls socket(2).

Create a new IPv6 TCP socket.

This calls socket(2).

Bind addr to the TCP socket.

Connect the socket to addr.

This consumes the socket and performs the connect operation. Once the connection completes, the socket is now a non-blocking TcpStream and can be used as such.

Listen for inbound connections, converting the socket to a TcpListener.

Sets the value of SO_REUSEADDR on this socket.

Get the value of SO_REUSEADDR set on this socket.

This is supported on Unix and neither Solaris nor illumos only.

Sets the value of SO_REUSEPORT on this socket. Only supported available in unix

This is supported on Unix and neither Solaris nor illumos only.

Get the value of SO_REUSEPORT set on this socket. Only supported available in unix

Sets the value of SO_LINGER on this socket.

Gets the value of SO_LINGER on this socket

Sets the value of SO_RCVBUF on this socket.

Get the value of SO_RCVBUF set on this socket.

Note that if set_recv_buffer_size has been called on this socket previously, the value returned by this function may not be the same as the argument provided to set_recv_buffer_size. This is for the following reasons:

  • Most operating systems have minimum and maximum allowed sizes for the receive buffer, and will clamp the provided value if it is below the minimum or above the maximum. The minimum and maximum buffer sizes are OS-dependent.
  • Linux will double the buffer size to account for internal bookkeeping data, and returns the doubled value from getsockopt(2). As per man 7 socket:

    Sets or gets the maximum socket receive buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2).

Sets the value of SO_SNDBUF on this socket.

Get the value of SO_SNDBUF set on this socket.

Note that if set_send_buffer_size has been called on this socket previously, the value returned by this function may not be the same as the argument provided to set_send_buffer_size. This is for the following reasons:

  • Most operating systems have minimum and maximum allowed sizes for the receive buffer, and will clamp the provided value if it is below the minimum or above the maximum. The minimum and maximum buffer sizes are OS-dependent.
  • Linux will double the buffer size to account for internal bookkeeping data, and returns the doubled value from getsockopt(2). As per man 7 socket:

    Sets or gets the maximum socket send buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2).

Sets whether keepalive messages are enabled to be sent on this socket.

This will set the SO_KEEPALIVE option on this socket.

Returns whether or not TCP keepalive probes will be sent by this socket.

Sets parameters configuring TCP keepalive probes for this socket.

The supported parameters depend on the operating system, and are configured using the TcpKeepalive struct. At a minimum, all systems support configuring the keepalive time: the time after which the OS will start sending keepalive messages on an idle connection.

Notes
  • This will enable TCP keepalive on this socket, if it is not already enabled.
  • On some platforms, such as Windows, any keepalive parameters not configured by the TcpKeepalive struct passed to this function may be overwritten with their default values. Therefore, this function should either only be called once per socket, or the same parameters should be passed every time it is called.
Examples
use mio::net::{TcpSocket, TcpKeepalive};
use std::time::Duration;

let socket = TcpSocket::new_v6()?;
let keepalive = TcpKeepalive::default()
    .with_time(Duration::from_secs(4));
    // Depending on the target operating system, we may also be able to
    // configure the keepalive probe interval and/or the number of retries
    // here as well.

socket.set_keepalive_params(keepalive)?;
This is supported on non-Windows only.

Returns the amount of time after which TCP keepalive probes will be sent on idle connections.

If None, then keepalive messages are disabled.

This returns the value of SO_KEEPALIVE + IPPROTO_TCP on OpenBSD, NetBSD, and Haiku, TCP_KEEPALIVE on macOS and iOS, and TCP_KEEPIDLE on all other Unix operating systems. On Windows, it is not possible to access the value of TCP keepalive parameters after they have been set.

Some platforms specify this value in seconds, so sub-second specifications may be omitted.

This is supported on Linux or macOS or iOS or FreeBSD or NetBSD only.

Returns the time interval between TCP keepalive probes, if TCP keepalive is enabled on this socket.

If None, then keepalive messages are disabled.

This returns the value of TCP_KEEPINTVL on supported Unix operating systems. On Windows, it is not possible to access the value of TCP keepalive parameters after they have been set..

Some platforms specify this value in seconds, so sub-second specifications may be omitted.

This is supported on Linux or macOS or iOS or FreeBSD or NetBSD only.

Returns the maximum number of TCP keepalive probes that will be sent before dropping a connection, if TCP keepalive is enabled on this socket.

If None, then keepalive messages are disabled.

This returns the value of TCP_KEEPCNT on Unix operating systems that support this option. On Windows, it is not possible to access the value of TCP keepalive parameters after they have been set.

Returns the local address of this socket

Will return Err result in windows if called before calling bind

Trait Implementations

Extracts the raw file descriptor. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Converts a RawFd to a TcpSocket.

Notes

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.