Using Rust's TcpStream


Reading and Writing Data

with Rust's TcpStream

Jun - 2020 (~7 minutes read time)

For the start of our journey with TcpStream we'll take a look at what it takes to send and receive raw bytes.

This demo is going to build up some concepts that result in a demo found here. The working concept is that we want to have a client send some data (a String) to a server that will echo the message back to the client. In further demos, we'll progress this concept to do slightly more interesting things with the data on the server before sending back.


Building a LinesCodec

for Rust's TcpStream

Jun - 2020 (~5 minutes read time)

In the previous post we learned how to read & write bytes with TcpStream, but the calling code had to be aware of that and do the serialization/deserialization. In this demo, we'll continue using BufRead and BufReader to build:

  • A LinesCodec that abstracts away String serialization/deserialization & TcpStream I/O
  • A client that uses the LinesCodec to send and print returned Strings
  • A server that also uses the LinesCodec and reverses Strings before echoing them back

Create a Custom Protocol

for Rust's TcpStream

Jun - 2020 (~8 minutes read time)

In this series so far we've learned how to read & write bytes with TcpStream and then how to abstract over that with a LinesCodec for sending and receiving String messages. In this post we'll look into what it takes to build a custom protocol for message passing more than a single type of thing (like a String).

Defining our Message Structs

To give our client and server more options for communicating we'll create:

  • A Request message that allow the client to request either:
    • Echo a string
    • Jumble a string with a specified amount of jumbling entropy
  • A Response message for the server to respond with the successfully echo/jumbled String