Transmission Control Protocol (TCP) is a primary communication protocol of the internet, though that is changing with HTTP3 (which is not built on TCP) gaining adoption.
TCP is great because it allows ordered data to be safely sent across the internet. For example, let's say we want to send the message "i am live":
| text | binary |
|---|---|
| i | 01101001 |
| a | 01100001 |
| m | 01101101 |
| l | 01101100 |
| i | 01101001 |
| v | 01110110 |
| e | 01100101 |
When data is sent over a network, it is sent in packets. Each message is split into packets, the packets are sent, they arrive (potentially) out of order, and they are reassembled on the other side. And without a protocol like TCP, you can't guarantee that the order is correct...
You might end up with "i am evil" instead of "i am live"! TCP solves this problem.
We're building our own HTTP, not our own TCP. We'll use the Go standard library's TCP implementation. Luckily Go doesn't have a built-in HTTP package or TheStartup™ would be out of business... /s
Instead of reading lines from a file, our program will now read lines from a TCP connection.
.Close() the listener when the program exits.When printing lines to the console, use fmt.Println() rather than log.Println(). The log package writes to stderr by default, which won't be captured by the tee command used in testing.
go run . | tee /tmp/tcp.txt
Run and submit the CLI tests while the server is running.
For linux users, the tests expect openbsd-netcat, not gnu-netcat.
We haven't shipped a way to customize the port on these CLI shell-command lesson types... admittedly, Prime is probably the only person that might have conflicts on port 42069, so it should be fine.