liveBook · Manning

TCP

As explained in Rust Servers, Services, and Apps

Transmission Control Protocol is a connection-oriented transport protocol that provides reliable, ordered delivery of a byte stream between endpoints.

TCP is a transport-layer protocol in the TCP/IP model. It carries a continuous stream of bytes between connected endpoints. An application protocol such as HTTP gives those bytes meaning; TCP itself does not interpret HTTP requests or responses. section 2.1 section 2.4

A basic Rust TCP exchange uses two standard-library types:

  • The server binds a TcpListener to an IP address and port, then iterates over incoming connections. section 2.2.1
  • The client connects with TcpStream. Each accepted connection also provides a TcpStream, which both sides use to read and write raw bytes. section 2.2.1

A local echo exchange

Bind the server to 127.0.0.1:3000 and connect a client to localhost:3000. First check that the server reports the connection. Then have the client write "Hello", the server read it into a byte buffer and write it back, and the client print the response. section 2.2.2

Listing 2.4 Completing the TCP server
use std::io::{Read, Write};                          #1
use std::net::TcpListener;
fn main() {
    let connection_listener = TcpListener::bind("127.0.0.1:3000").unwrap();
    println!("Running on port 3000");
    for stream in connection_listener.incoming() {
        let mut stream = stream.unwrap();            #2
        println!("Connection established");
        let mut buffer = [0; 1024];
        stream.read(&mut buffer).unwrap();           #3
        stream.write(&mut buffer).unwrap();          #4
    }
}

The server listing shows the transition from accepting a connection to reading and writing its byte stream. section 2.2.2

Listing 2.5 Completing the TCP client
use std::io::{Read, Write};
use std::net::TcpStream;
use std::str;
 
fn main() {
    let mut stream = TcpStream::connect("localhost:3000").unwrap();
    stream.write("Hello".as_bytes()).unwrap();       #1
    let mut buffer = [0; 5];
    stream.read(&mut buffer).unwrap();               #2
    println!(
        "Got response from server:{:?}",             #3
        str::from_utf8(&buffer).unwrap()
    );
}

The matching client listing connects, sends bytes, reads the reply, and converts it to text for display. section 2.2.2

The echo exchange establishes the transport without defining an application message format. An HTTP server adds that layer by parsing requests and constructing responses. In a P2P network, a peer may also need to initiate and accept connections across multiple protocols. section 2.4 section 11.1.1

How Rust Servers, Services, and Apps covers it

How deep it goes
Works through binding, connecting, reading, writing, and echoing bytes, but does not elaborate TCP reliability mechanisms.
Examples
Uses a localhost Rust client and server that connect on port 3000 and echo "Hello".
What it assumes
Assumes familiarity with Rust projects while introducing the networking primitives needed for a TCP exchange.
What you can do after
Build it from scratch: create a basic Rust TCP client and server that exchange raw bytes.
How it connects
Links TCP to the TCP/IP model, IP addresses, HTTP, and P2P transport choices alongside UDP.

Also explained in

Compare how 5 books explain TCP →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.