Struct websocket::stream::sync::TcpStream 1.0.0
[−]
[src]
pub struct TcpStream(_);
A TCP stream between a local and a remote socket.
After creating a TcpStream
by either connect
ing to a remote host or
accept
ing a connection on a TcpListener
, data can be transmitted
by reading and writing to it.
The connection will be closed when the value is dropped. The reading and writing
portions of the connection can also be shut down individually with the shutdown
method.
The Transmission Control Protocol is specified in IETF RFC 793.
Examples
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
[src]
fn connect<A>(addr: A) -> Result<TcpStream, Error> where
A: ToSocketAddrs,
A: ToSocketAddrs,
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.
In case ToSocketAddrs::to_socket_addrs()
returns more than one entry,
then the first valid and reachable address is used.
Examples
use std::net::TcpStream; if let Ok(stream) = TcpStream::connect("127.0.0.1:8080") { println!("Connected to the server!"); } else { println!("Couldn't connect to server..."); }
fn peer_addr(&self) -> Result<SocketAddr, Error>
Returns the socket address of the remote peer of this TCP connection.
Examples
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream}; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); assert_eq!(stream.peer_addr().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
fn local_addr(&self) -> Result<SocketAddr, Error>
Returns the socket address of the local half of this TCP connection.
Examples
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream}; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); assert_eq!(stream.local_addr().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
fn shutdown(&self, how: Shutdown) -> Result<(), Error>
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
).
Platform-specific behavior
Calling this function multiple times may result in different behavior,
depending on the operating system. On Linux, the second call will
return Ok(())
, but on macOS, it will return ErrorKind::NotConnected
.
This may change in the future.
Examples
use std::net::{Shutdown, TcpStream}; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.shutdown(Shutdown::Both).expect("shutdown call failed");
fn try_clone(&self) -> Result<TcpStream, Error>
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.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); let stream_clone = stream.try_clone().expect("clone failed...");
fn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
1.4.0
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
.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.set_read_timeout(None).expect("set_read_timeout call failed");
fn set_write_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
1.4.0
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
.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.set_write_timeout(None).expect("set_write_timeout call failed");
fn read_timeout(&self) -> Result<Option<Duration>, Error>
1.4.0
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.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.set_read_timeout(None).expect("set_read_timeout call failed"); assert_eq!(stream.read_timeout().unwrap(), None);
fn write_timeout(&self) -> Result<Option<Duration>, Error>
1.4.0
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.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.set_write_timeout(None).expect("set_write_timeout call failed"); assert_eq!(stream.write_timeout().unwrap(), None);
fn peek(&self, buf: &mut [u8]) -> Result<usize, Error>
1.18.0
Receives data on the socket from the remote adress to which it is connected, without removing that data from the queue. On success, returns the number of bytes peeked.
Successive calls return the same data. This is accomplished by passing
MSG_PEEK
as a flag to the underlying recv
system call.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8000") .expect("couldn't bind to address"); let mut buf = [0; 10]; let len = stream.peek(&mut buf).expect("peek failed");
fn set_nodelay(&self, nodelay: bool) -> Result<(), Error>
1.9.0
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.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.set_nodelay(true).expect("set_nodelay call failed");
fn nodelay(&self) -> Result<bool, Error>
1.9.0
Gets the value of the TCP_NODELAY
option on this socket.
For more information about this option, see set_nodelay
.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.set_nodelay(true).expect("set_nodelay call failed"); assert_eq!(stream.nodelay().unwrap_or(false), true);
fn set_ttl(&self, ttl: u32) -> Result<(), Error>
1.9.0
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.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.set_ttl(100).expect("set_ttl call failed");
fn ttl(&self) -> Result<u32, Error>
1.9.0
Gets the value of the IP_TTL
option for this socket.
For more information about this option, see set_ttl
.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.set_ttl(100).expect("set_ttl call failed"); assert_eq!(stream.ttl().unwrap_or(0), 100);
fn take_error(&self) -> Result<Option<Error>, Error>
1.9.0
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.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.take_error().expect("No error was expected...");
fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
1.9.0
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.
Examples
use std::net::TcpStream; let stream = TcpStream::connect("127.0.0.1:8080") .expect("Couldn't connect to the server..."); stream.set_nonblocking(true).expect("set_nonblocking call failed");
Trait Implementations
impl<'a> Write for &'a TcpStream
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Write a buffer into this object, returning how many bytes were written. Read more
fn flush(&mut self) -> Result<(), Error>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Write
. Read more
impl Write for TcpStream
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Write a buffer into this object, returning how many bytes were written. Read more
fn flush(&mut self) -> Result<(), Error>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Write
. Read more
impl Debug for TcpStream
[src]
impl FromRawFd for TcpStream
1.1.0[src]
unsafe fn from_raw_fd(fd: i32) -> TcpStream
impl Read for TcpStream
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0
Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Read
. Read more
fn bytes(self) -> Bytes<Self>
Transforms this Read
instance to an Iterator
over its bytes. Read more
fn chars(self) -> Chars<Self>
🔬 This is a nightly-only experimental API. (io
)
the semantics of a partial read/write of where errors happen is currently unclear and may change
Transforms this Read
instance to an Iterator
over char
s. Read more
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
R: Read,
Creates an adaptor which will chain this stream with another. Read more
fn take(self, limit: u64) -> Take<Self>
Creates an adaptor which will read at most limit
bytes from it. Read more
impl<'a> Read for &'a TcpStream
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0
Read the exact number of bytes required to fill buf
. Read more
fn by_ref(&mut self) -> &mut Self
Creates a "by reference" adaptor for this instance of Read
. Read more
fn bytes(self) -> Bytes<Self>
Transforms this Read
instance to an Iterator
over its bytes. Read more
fn chars(self) -> Chars<Self>
🔬 This is a nightly-only experimental API. (io
)
the semantics of a partial read/write of where errors happen is currently unclear and may change
Transforms this Read
instance to an Iterator
over char
s. Read more
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
R: Read,
Creates an adaptor which will chain this stream with another. Read more
fn take(self, limit: u64) -> Take<Self>
Creates an adaptor which will read at most limit
bytes from it. Read more
impl IntoRawFd for TcpStream
1.4.0[src]
fn into_raw_fd(self) -> i32
impl AsRawFd for TcpStream
[src]
impl TcpStreamExt for TcpStream
[src]
fn set_recv_buffer_size(&self, size: usize) -> Result<(), Error>
fn recv_buffer_size(&self) -> Result<usize, Error>
fn set_send_buffer_size(&self, size: usize) -> Result<(), Error>
fn send_buffer_size(&self) -> Result<usize, Error>
fn set_nodelay(&self, nodelay: bool) -> Result<(), Error>
fn nodelay(&self) -> Result<bool, Error>
fn set_keepalive(&self, keepalive: Option<Duration>) -> Result<(), Error>
fn keepalive(&self) -> Result<Option<Duration>, Error>
fn set_keepalive_ms(&self, keepalive: Option<u32>) -> Result<(), Error>
fn keepalive_ms(&self) -> Result<Option<u32>, Error>
fn set_read_timeout_ms(&self, dur: Option<u32>) -> Result<(), Error>
fn read_timeout_ms(&self) -> Result<Option<u32>, Error>
fn set_write_timeout_ms(&self, dur: Option<u32>) -> Result<(), Error>
fn write_timeout_ms(&self) -> Result<Option<u32>, Error>
fn set_read_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
fn read_timeout(&self) -> Result<Option<Duration>, Error>
fn set_write_timeout(&self, dur: Option<Duration>) -> Result<(), Error>
fn write_timeout(&self) -> Result<Option<Duration>, Error>
fn set_ttl(&self, ttl: u32) -> Result<(), Error>
fn ttl(&self) -> Result<u32, Error>
fn set_only_v6(&self, only_v6: bool) -> Result<(), Error>
fn only_v6(&self) -> Result<bool, Error>
fn connect<T>(&self, addr: T) -> Result<(), Error> where
T: ToSocketAddrs,
T: ToSocketAddrs,
fn take_error(&self) -> Result<Option<Error>, Error>
fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
fn set_linger(&self, dur: Option<Duration>) -> Result<(), Error>
fn linger(&self) -> Result<Option<Duration>, Error>
impl Splittable for TcpStream
[src]
type Reader = TcpStream
The reading component of this type
type Writer = TcpStream
The writing component of this type
fn split(self) -> Result<(TcpStream, TcpStream)>
Split apart this type into a reading and writing component.