tcp-stream

Crates.iotcp-stream
lib.rstcp-stream
version0.29.2
sourcesrc
created_at2019-05-23 06:57:45.404498
updated_at2024-08-20 20:41:18.10857
descriptionstd::net::TcpStream on steroids
homepage
repositoryhttps://github.com/amqp-rs/tcp-stream
max_upload_size
id136305
size36,041
Marc-Antoine Perennou (Keruspe)

documentation

https://docs.rs/tcp-stream

README

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));
}
Commit count: 192

cargo fmt