pub struct TcpStream { /* private fields */ }Expand description
A TCP stream between a local and a remote socket.
The socket will be closed when the value is dropped.
The read and write methods can be interrupted using the Canceller object created with the stream.
Otherwise similar to std::net::TcpStream.
Implementations§
Source§impl TcpStream
impl TcpStream
Sourcepub fn connect<A: ToSocketAddrs>(address: A) -> Result<(Self, Canceller)>
pub fn connect<A: ToSocketAddrs>(address: A) -> Result<(Self, Canceller)>
Creates a new TCP stream with an object used to cancel read/write operations, and connects it to a remote address.
Examples found in repository?
7fn main() {
8 let (listener, listener_canceller) = TcpListener::bind("127.0.0.1:0").unwrap();
9 let address = listener.local_addr().unwrap();
10 let server = thread::spawn(move || {
11 println!("Server: ready");
12 let (mut socket, socket_canceller, addr) = listener.accept().unwrap();
13 println!("Server: got connection from {}", addr);
14
15 let connection = thread::spawn(move || {
16 println!("Connection: ready");
17 let mut buf = [0; 16];
18 let len = socket.read(&mut buf).unwrap();
19 println!("Connection: read {}", String::from_utf8_lossy(&buf[..len]));
20 println!("Connection: try reading more.");
21 match socket.read(&mut buf) {
22 Ok(0) => println!("Connection: socket closed."),
23 Err(ref e) if is_cancelled(e) => println!("Connection: cancelled."),
24 ref e => panic!("Connection: unexpected {:?}", e),
25 }
26 });
27
28 println!("Server: try accepting more.");
29 if is_cancelled(&listener.accept().unwrap_err()) {
30 println!("Server: accept cancelled.");
31 }
32
33 socket_canceller.cancel().unwrap();
34 connection.join().unwrap();
35 });
36
37 thread::sleep(Duration::from_secs(2));
38 let (mut socket, socket_canceller) = TcpStream::connect(&address).unwrap();
39 let client = thread::spawn(move || {
40 println!("Client: ready");
41 thread::sleep(Duration::from_secs(2));
42 println!("Client: write data.");
43 socket.write(b"Hello!").unwrap();
44 println!("Client: try reading.");
45 let mut buf = [0; 16];
46 match socket.read(&mut buf) {
47 Ok(0) => println!("Client: socket closed."),
48 Err(ref e) if is_cancelled(e) => println!("Client: cancelled."),
49 ref e => panic!("Client: unexpected {:?}", e),
50 }
51 });
52
53 thread::sleep(Duration::from_secs(4));
54 socket_canceller.cancel().unwrap();
55 thread::sleep(Duration::from_secs(2));
56 listener_canceller.cancel().unwrap();
57
58 server.join().unwrap();
59 client.join().unwrap();
60}Sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
Returns the socket address of the remote peer of this TCP connection.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Returns the socket address of the local half of this TCP connection.
Sourcepub fn shutdown(&self, how: Shutdown) -> Result<()>
pub fn shutdown(&self, how: Shutdown) -> Result<()>
Shuts down the read, write, or both halves of this connection.
Sourcepub fn try_clone(&self) -> Result<(Self, Canceller)>
pub fn try_clone(&self) -> Result<(Self, Canceller)>
Creates a new independently owned handle to the underlying socket. The Cancellers associated with the original object and its clone are also independent.
Sourcepub fn set_read_timeout(&self, duration: Option<Duration>) -> Result<()>
pub fn set_read_timeout(&self, duration: Option<Duration>) -> Result<()>
Sets the read timeout.
Sourcepub fn set_write_timeout(&self, duration: Option<Duration>) -> Result<()>
pub fn set_write_timeout(&self, duration: Option<Duration>) -> Result<()>
Sets the write timeout.
Sourcepub fn read_timeout(&self) -> Result<Option<Duration>>
pub fn read_timeout(&self) -> Result<Option<Duration>>
Gets the read timeout.
Sourcepub fn write_timeout(&self) -> Result<Option<Duration>>
pub fn write_timeout(&self) -> Result<Option<Duration>>
Gets the write timeout.
Sourcepub fn set_nodelay(&self, nodelay: bool) -> Result<()>
pub fn set_nodelay(&self, nodelay: bool) -> Result<()>
Sets the value of the TCP_NODELAY option on this socket.
Sourcepub fn nodelay(&self) -> Result<bool>
pub fn nodelay(&self) -> Result<bool>
Gets the value of the TCP_NODELAY option for this socket.
Sourcepub fn set_ttl(&self, ttl: u32) -> Result<()>
pub fn set_ttl(&self, ttl: u32) -> Result<()>
Sets the value for the IP_TTL option on this socket.
Sourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
Gets the value of the SO_ERROR option for this socket.
Sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
Moves this TCP stream into or out of nonblocking mode.
Trait Implementations§
Source§impl Read for TcpStream
impl Read for TcpStream
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Reads data from the socket. This operation can be cancelled by the associated Canceller object.
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
Source§impl Write for TcpStream
impl Write for TcpStream
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Writes data to the socket. This operation can be cancelled by the associated Canceller object.
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)