96 releases (34 breaking)

Uses new Rust 2024

new 0.34.8 May 5, 2026
0.34.5 Apr 18, 2026
0.34.3 Mar 24, 2026
0.34.0 Sep 13, 2025
0.4.1 Jun 26, 2019

#301 in Network programming

Download history 114492/week @ 2026-01-18 131280/week @ 2026-01-25 150757/week @ 2026-02-01 173210/week @ 2026-02-08 168298/week @ 2026-02-15 206874/week @ 2026-02-22 215813/week @ 2026-03-01 238297/week @ 2026-03-08 224428/week @ 2026-03-15 174608/week @ 2026-03-22 165785/week @ 2026-03-29 178338/week @ 2026-04-05 194532/week @ 2026-04-12 193227/week @ 2026-04-19 179846/week @ 2026-04-26 144160/week @ 2026-05-03

724,590 downloads per month
Used in 199 crates (2 directly)

BSD-2-Clause

45KB
886 lines

std::net::TcpStream on steroids

API Docs Build status Downloads

tcp-stream is a library aiming at providing TLS support to std::net::TcpStream

Warning about crypto backends for rustls

A crypto implementation must be enabled in rustls using feature flags. We mimic what rustls does, providing one feature flag per implementation and enabling the same as rustls by default. Available options are:

  • rustls--aws_lc_rs (default)
  • rustls--ring

Examples

To connect to a remote server:

use tcp_stream::{HandshakeError, TcpStream, TLSConfig};

use std::io::{self, Read, Write};

fn main() {
    let stream = TcpStream::connect("google.com:443").unwrap();
    let mut stream = stream.into_tls("google.com", TLSConfig::default());

    while let Err(HandshakeError::WouldBlock(mid_handshake)) = stream {
        stream = mid_handshake.handshake();
    }

    let mut stream = stream.unwrap();

    while let Err(err) = stream.write_all(b"GET / HTTP/1.0\r\n\r\n") {
        if err.kind() != io::ErrorKind::WouldBlock {
            panic!("error: {:?}", err);
        }
    }
    stream.flush().unwrap();
    let mut res = vec![];
    while let Err(err) = stream.read_to_end(&mut res) {
        if err.kind() != io::ErrorKind::WouldBlock {
            panic!("stream error: {:?}", err);
        }
    }
    println!("{}", String::from_utf8_lossy(&res));
}

Dependencies

~0–29MB
~494K SLoC