# Struct std::net::TcpStream [−] [src]

    pub struct TcpStream(_);

1.0.0

A structure which represents a TCP stream between a local socket and a remote socket.

The socket will be closed when the value is dropped.

# Examples

fn main() { use std::io::prelude::*; use std::net::TcpStream; { let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap(); // ignore the Result let _ = stream.write(&[1]); let _ = stream.read(&mut [0; 128]); // ignore here too } // the stream is closed here }

    use std::io::prelude::*;
    use std::net::TcpStream;

    {
        let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();

        // ignore the Result
        let _ = stream.write(&[1]);
        let _ = stream.read(&mut [0; 128]); // ignore here too
    } // the stream is closed here

## Methods

### `impl TcpStream`

#### `fn connect<A: ToSocketAddrs>(addr: A) -> Result<TcpStream>`

Opens a TCP connection to a remote host.

`addr` is an address of the remote host. Anything which implements `ToSocketAddrs` trait can be supplied for the address; see this trait documentation for concrete examples.

#### `fn peer_addr(&self) -> Result<SocketAddr>`

Returns the socket address of the remote peer of this TCP connection.

#### `fn local_addr(&self) -> Result<SocketAddr>`

Returns the socket address of the local half of this TCP connection.

#### `fn shutdown(&self, how: Shutdown) -> Result<()>`

Shuts down the read, write, or both halves of this connection.

This function will cause all pending and future I/O on the specified portions to return immediately with an appropriate value (see the documentation of `Shutdown`).

#### `fn try_clone(&self) -> Result<TcpStream>`

Creates a new independently owned handle to the underlying socket.

The returned `TcpStream` is a reference to the same stream that this object references. Both handles will read and write the same stream of data, and options set on one stream will be propagated to the other stream.

#### 1.4.0 `fn set_read_timeout(&self, dur: Option<Duration>) -> Result<()>`

Sets the read timeout to the timeout specified.

If the value specified is `None`, then `read` calls will block indefinitely. It is an error to pass the zero `Duration` to this method.

# Note

Platforms may return a different error code whenever a read times out as a result of setting this option. For example Unix typically returns an error of the kind `WouldBlock`, but Windows may return `TimedOut`.

#### 1.4.0 `fn set_write_timeout(&self, dur: Option<Duration>) -> Result<()>`

Sets the write timeout to the timeout specified.

If the value specified is `None`, then `write` calls will block indefinitely. It is an error to pass the zero `Duration` to this method.

# Note

Platforms may return a different error code whenever a write times out as a result of setting this option. For example Unix typically returns an error of the kind `WouldBlock`, but Windows may return `TimedOut`.

#### 1.4.0 `fn read_timeout(&self) -> Result<Option<Duration>>`

Returns the read timeout of this socket.

If the timeout is `None`, then `read` calls will block indefinitely.

# Note

Some platforms do not provide access to the current timeout.

#### 1.4.0 `fn write_timeout(&self) -> Result<Option<Duration>>`

Returns the write timeout of this socket.

If the timeout is `None`, then `write` calls will block indefinitely.

# Note

Some platforms do not provide access to the current timeout.

#### 1.9.0 `fn set_nodelay(&self, nodelay: bool) -> Result<()>`

Sets the value of the `TCP_NODELAY` option on this socket.

If set, this option disables the Nagle algorithm. This means that segments are always sent as soon as possible, even if there is only a small amount of data. When not set, data is buffered until there is a sufficient amount to send out, thereby avoiding the frequent sending of small packets.

#### 1.9.0 `fn nodelay(&self) -> Result<bool>`

Gets the value of the `TCP_NODELAY` option on this socket.

For more information about this option, see `set_nodelay`.

#### 1.9.0 `fn set_ttl(&self, ttl: u32) -> Result<()>`

Sets the value for the `IP_TTL` option on this socket.

This value sets the time-to-live field that is used in every packet sent from this socket.

#### 1.9.0 `fn ttl(&self) -> Result<u32>`

Gets the value of the `IP_TTL` option for this socket.

For more information about this option, see `set_ttl`.

#### 1.9.0 `fn set_only_v6(&self, only_v6: bool) -> Result<()>`

Sets the value for the `IPV6_V6ONLY` option on this socket.

If this is set to `true` then the socket is restricted to sending and receiving IPv6 packets only. If this is the case, an IPv4 and an IPv6 application can each bind the same port at the same time.

If this is set to `false` then the socket can be used to send and receive packets from an IPv4-mapped IPv6 address.

#### 1.9.0 `fn only_v6(&self) -> Result<bool>`

Gets the value of the `IPV6_V6ONLY` option for this socket.

For more information about this option, see `set_only_v6`.

#### 1.9.0 `fn take_error(&self) -> Result<Option<Error>>`

Get 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.

#### 1.9.0 `fn set_nonblocking(&self, nonblocking: bool) -> Result<()>`

Moves this TCP stream into or out of nonblocking mode.

On Unix this corresponds to calling fcntl, and on Windows this corresponds to calling ioctlsocket.

## Trait Implementations

### `impl Read for TcpStream`

#### `fn read(&mut self, buf: &mut [u8]) -> Result<usize>`

#### `fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>`

#### `fn read_to_string(&mut self, buf: &mut String) -> Result<usize>`

#### 1.6.0 `fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>`

#### `fn by_ref(&mut self) -> &mut Self where Self: Sized`

#### `fn bytes(self) -> Bytes<Self> where Self: Sized`

#### `fn chars(self) -> Chars<Self> where Self: Sized`

#### `fn chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized`

#### `fn take(self, limit: u64) -> Take<Self> where Self: Sized`

### `impl Write for TcpStream`

#### `fn write(&mut self, buf: &[u8]) -> Result<usize>`

#### `fn flush(&mut self) -> Result<()>`

#### `fn write_all(&mut self, buf: &[u8]) -> Result<()>`

#### `fn write_fmt(&mut self, fmt: Arguments) -> Result<()>`

#### `fn by_ref(&mut self) -> &mut Self where Self: Sized`

### `impl<'a> Read for &'a TcpStream`

#### `fn read(&mut self, buf: &mut [u8]) -> Result<usize>`

#### `fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>`

#### `fn read_to_string(&mut self, buf: &mut String) -> Result<usize>`

#### 1.6.0 `fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>`

#### `fn by_ref(&mut self) -> &mut Self where Self: Sized`

#### `fn bytes(self) -> Bytes<Self> where Self: Sized`

#### `fn chars(self) -> Chars<Self> where Self: Sized`

#### `fn chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized`

#### `fn take(self, limit: u64) -> Take<Self> where Self: Sized`

### `impl<'a> Write for &'a TcpStream`

#### `fn write(&mut self, buf: &[u8]) -> Result<usize>`

#### `fn flush(&mut self) -> Result<()>`

#### `fn write_all(&mut self, buf: &[u8]) -> Result<()>`

#### `fn write_fmt(&mut self, fmt: Arguments) -> Result<()>`

#### `fn by_ref(&mut self) -> &mut Self where Self: Sized`

### `impl Debug for TcpStream`

#### `fn fmt(&self, f: &mut Formatter) -> Result`

### `impl AsRawFd for TcpStream`

#### `fn as_raw_fd(&self) -> RawFd`

### `impl FromRawFd for TcpStream` 1.1.0

#### `unsafe fn from_raw_fd(fd: RawFd) -> TcpStream`

### `impl IntoRawFd for TcpStream` 1.4.0

#### `fn into_raw_fd(self) -> RawFd`

### `impl AsRawFd for TcpStream`

#### `fn as_raw_fd(&self) -> RawFd`

### `impl FromRawFd for TcpStream` 1.1.0

#### `unsafe fn from_raw_fd(fd: RawFd) -> TcpStream`

### `impl IntoRawFd for TcpStream` 1.4.0

#### `fn into_raw_fd(self) -> RawFd`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)