Processing bytes using structs
In the previous chapter, we were sending strings to the server. However, the result is that we had to parse individual values into the type that we needed. As string parsing was not well structured, it is not clear to other developers what structure our messages are. We can make our message structure clearer by defining a struct that can be sent over the TCP channel. Sending a struct over a TCP channel can be achieved by converting the message struct into a binary format before sending the struct itself. This is also known as serializing the data.
If we are to convert a struct into a binary format, first, we will need to utilize the serde
and bincode
crates. With our new crates, both the client and server Cargo.toml
file should contain the following dependencies:
[dependencies] serde = { version = "1.0.144", features = ["derive"] } tokio = { version = "1", features = ["full"] } bincode = "1.3.3"...