Struct std::net::TcpStream
A TCP connection
Example
use std::net::{NameLookup, TcpStream, Shutdown};
use std::io::{Result, StdioStream, copy};
fn http_fetch() -> Result<()> {
let addrs = NameLookup::resolve("docs.alumina-lang.net", 80)?;
defer addrs.free();
let socket = TcpStream::connect(&addrs.next().unwrap())?;
defer socket.close();
socket.write_all("GET / HTTP/1.1\r\n")?;
socket.write_all("Host: docs.alumina-lang.net\r\n")?;
socket.write_all("Connection: close\r\n\r\n")?;
socket.shutdown(Shutdown::Write)?;
copy(&socket, &StdioStream::stdout())?;
Result::ok(())
}
http_fetch().unwrap();
Run this example
Fields
-
socket: Socket
Methods
impl TcpStream { ... }
-
fn connect(addr: &SocketAddr) -> Result<TcpStream>
Establishes a TCP connection to a remote host.
-
Create a new TCP stream from a raw socket
-
fn as_fd(self: &TcpStream) -> FileDescriptor
-
fn peer_addr(self: &TcpStream) -> Result<SocketAddr>
Returns the address of the remote peer
-
fn socket_addr(self: &TcpStream) -> Result<SocketAddr>
Returns the address of the remote peer
-
Shuts down the reading or writing end of the socket or both.
-
Sets the time-to-live (TTL) value for the socket.
This value controls the number of hops (routers) that packets can traverse before being discarded. Works for both IPv4 and IPv6 sockets.
Example
use std::net::{TcpStream, SocketAddr}; let addr = SocketAddr::parse("93.184.216.34:80").unwrap(); let stream = TcpStream::connect(&addr).unwrap(); defer stream.close(); stream.set_ttl(64).unwrap(); assert_eq!(stream.ttl().unwrap(), 64);
Run this example -
Gets the time-to-live (TTL) value for the socket.
Works for both IPv4 and IPv6 sockets.
Example
use std::net::{TcpStream, SocketAddr}; let addr = SocketAddr::parse("93.184.216.34:80").unwrap(); let stream = TcpStream::connect(&addr).unwrap(); defer stream.close(); let ttl = stream.ttl().unwrap(); println!("TTL: {}", ttl);
Run this example -
Sets the value of the
TCP_NODELAY
option for this socket.When enabled, this option disables the Nagle algorithm, which batches small writes to reduce network overhead. Disabling Nagle (nodelay = true) reduces latency but may reduce throughput.
Example
use std::net::{TcpStream, SocketAddr}; let addr = SocketAddr::parse("[::1]:8080").unwrap(); let stream = TcpStream::connect(&addr).unwrap(); defer stream.close(); stream.set_nodelay(true).unwrap(); assert_eq!(stream.nodelay().unwrap(), true);
Run this example -
Gets the value of the
TCP_NODELAY
option for this socket.Example
use std::net::{TcpStream, SocketAddr}; let addr = SocketAddr::parse("[::1]:8080").unwrap(); let stream = TcpStream::connect(&addr).unwrap(); defer stream.close(); let nodelay = stream.nodelay().unwrap(); println!("TCP_NODELAY: {}", nodelay);
Run this example -
Sets the read timeout for the socket.
If the provided duration is
Option::none()
, then read operations will block indefinitely. Otherwise, read operations will time out after the specified duration.Example
use std::net::{TcpStream, SocketAddr}; use std::time::Duration; let addr = SocketAddr::parse("[::1]:8080").unwrap(); let stream = TcpStream::connect(&addr).unwrap(); defer stream.close(); stream.set_read_timeout(Option::some(Duration::from_secs(5))).unwrap();
Run this example -
Gets the read timeout for the socket.
Returns
Option::none()
if reads will block indefinitely.Example
use std::net::{TcpStream, SocketAddr}; let addr = SocketAddr::parse("[::1]:8080").unwrap(); let stream = TcpStream::connect(&addr).unwrap(); defer stream.close(); let timeout = stream.read_timeout().unwrap(); if timeout.is_some() { println!("Read timeout: {} seconds", timeout.unwrap().total_secs()); }
Run this example -
Sets the write timeout for the socket.
If the provided duration is
Option::none()
, then write operations will block indefinitely. Otherwise, write operations will time out after the specified duration.Example
use std::net::{TcpStream, SocketAddr}; use std::time::Duration; let addr = SocketAddr::parse("[::1]:8080").unwrap(); let stream = TcpStream::connect(&addr).unwrap(); defer stream.close(); stream.set_write_timeout(Option::some(Duration::from_secs(5))).unwrap();
Run this example -
Gets the write timeout for the socket.
Returns
Option::none()
if writes will block indefinitely.Example
use std::net::{TcpStream, SocketAddr}; let addr = SocketAddr::parse("[::1]:8080").unwrap(); let stream = TcpStream::connect(&addr).unwrap(); defer stream.close(); let timeout = stream.write_timeout().unwrap(); if timeout.is_some() { println!("Write timeout: {} seconds", timeout.unwrap().total_secs()); }
Run this example -
Sets the socket to non-blocking or blocking mode.
In non-blocking mode, read and write operations will return immediately with an error if they would block.
Example
use std::net::{TcpStream, SocketAddr}; let addr = SocketAddr::parse("[::1]:8080").unwrap(); let stream = TcpStream::connect(&addr).unwrap(); defer stream.close(); stream.set_nonblocking(true).unwrap();
Run this example -
Reads from the stream into a provided buffer.
If successful, returns the number of bytes read. This may be less than the size of the provided buffer.
-
Write the buffer to the stream.
Returns the number of bytes written. This may be less than the size of the buffer. See also write_all, which can be used to write the entire buffer.
-
Flush the stream
This may be a no-op, depending on the stream.
-
Closes the socket.
Mixins
impl TcpStream { ... }
-
-
Fills the provided buffer by reading from the stream.
If stream had less bytes available than the size of the buffer, this method will return an
Error::eof()
. -
Reads the entire stream into a
StringBuf
.If successful, returns the number of bytes that were read.
-
-
Writes all the bytes in the buffer to the stream.
If EOF is reached, returns
Error::eof()
.