69 releases

0.29.1 Jun 22, 2024
0.28.0 Apr 24, 2024
0.27.1 Jan 2, 2024
0.27.0 Dec 14, 2023
0.4.1 Jun 26, 2019

#863 in Network programming

Download history 27668/week @ 2024-03-31 28413/week @ 2024-04-07 25413/week @ 2024-04-14 27297/week @ 2024-04-21 24113/week @ 2024-04-28 24778/week @ 2024-05-05 26162/week @ 2024-05-12 27341/week @ 2024-05-19 24780/week @ 2024-05-26 30761/week @ 2024-06-02 27986/week @ 2024-06-09 25554/week @ 2024-06-16 29127/week @ 2024-06-23 26205/week @ 2024-06-30 31257/week @ 2024-07-07 32413/week @ 2024-07-14

119,824 downloads per month
Used in 86 crates (2 directly)

BSD-2-Clause

33KB
606 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–19MB
~365K SLoC