Struct std::net::TcpStream
[−]
[src]
pub struct TcpStream(_);
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.
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
.
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
.
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.
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.