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.
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:
LinesCodec
that abstracts away String
serialization/deserialization & TcpStream I/OLinesCodec
to send and print returned String
sLinesCodec
and reverses String
s before echoing them backIn 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
).
To give our client and server more options for communicating we'll create:
Request
message that allow the client to request either:
Response
message for the server to respond with the successfully echo/jumbled String