Make TCP connections

This exercise guides you through establishing a raw TCP connection between two servers and manually sending an HTTP request to a web server, illustrating how network protocols work in practice.

📜 Legend

Parts of this exercise are annotated with the following icons:

  • ❗ A task you MUST perform to complete the exercise
  • ❓ Optional step that you may perform to make sure that everything is working correctly, or to set up additional tools that are not required but can help you, or optional extra exercises to explore the topic further
  • 👾 Advanced tips on how to go further (or challenges!)
  • 🏁 The end of the exercise
  • 🏛️ The architecture of the software you ran or deployed during this exercise
  • 💥 Troubleshooting tips: how to fix common problems you might encounter

❗ Establish a bi-directional TCP connection

🛠️

You need to find a partner for this part of the exercise, since the goal is to establish a TCP connection between two of the servers you have set up for the course.

Let’s call you and your partner Bob and Alice.

Alice will need to know the public IP address of Bob’s server. We will refer to it as W.X.Y.Z.

❗ Bob: listen for TCP clients

Bob should run the nc command on his server to listen for TCP connections on port 3000:

$> nc -l 3000
📚More information

At this point, nc will start listening on port 3000 and take over the console, waiting for a TCP client to connect.

❗ Alice: connect to Bob’s server

Alice should run the nc command on her server to connect to TCP port 3000 on Bob’s server:

$> nc W.X.Y.Z 3000
📚More information

Here, nc is acting as a client, connecting to the listening nc process on Bob’s server.

❗ Bob and Alice: communicate!

Bob should type “Hello” and press Enter to send this text:

$> nc -l 3000
Hello

It should be immediately displayed in Alice’s terminal:

$> nc W.X.Y.Z 3000
Hello

Similarly, if Alice types “World” after that and presses Enter, it should immediately appear in Bob’s terminal:

$> nc W.X.Y.Z 3000
Hello
World
$> nc -l 3000
Hello
World

You have a two-way raw TCP connection running.

Once you’re done, you can close the connection with Ctrl-C.

❗ Talk dirty HTTP to Google

🛠️

You can do the rest of this exercise individually.

Let’s do something that your browser does every day: an HTTP request.

Find out Google’s IP address (O.P.Q.R in this example) using the ping command:

$> ping -c 1 google.com
PING google.com (O.P.Q.R) 56(84) bytes of data.
64 bytes from google.com (O.P.Q.R): icmp_seq=1 ttl=53 time=0.890 ms
...

Open a TCP connection to the Google IP address you found:

$> nc O.P.Q.R 80

Now, talk to this Google server, but not in English or French, in the HTTP protocol. Type exactly the following text:

GET / HTTP/1.1
Host: www.google.com

Then press Enter twice and you should receive the HTML for Google’s home page.

💎Tip

Be sure to type exactly the text above. Your request must be a valid HTTP request or Google’s server will not be able to interpret it correctly. If you have made a mistake, exit with Ctrl-C, re-run the same nc command and try again.

Once you’re done, you can close the connection with Ctrl-C.

If you open your browser, visit http://www.google.com and display the source code of the page, you should see the same result.

📚More information

By sending this text over the TCP connection, you are communicating in the HTTP protocol, a text protocol: you are sending an HTTP request to retrieve (GET) the resource at path / of host www.google.com (the landing page of the Google website), using version 1.1 of the HTTP protocol.

🏁 What have I done?

Like the pioneers of the 1970s who developed TCP/IP, you connected two machines across the Internet and exchanged a few (hopefully nice) words. Then you did by hand what your browser does every time you open a page: you asked a web server for it.

A TCP connection always starts the same way: one process listens on a port and waits, and another connects to an address and that port. The one that connects is the client, the one that listens is the server. Bob’s nc was the server and Alice’s the client, yet both of you could type: once the connection is open, it carries data in both directions.

To reach Bob’s nc, three things had to be right. Alice needed the public address of Bob’s server, the one you connect to with SSH, and not the private one that ip address prints on the server itself. Azure’s firewall had to let port 3000 through, which is why you opened it when you created your server. And a process had to be listening on that port: an open port with nothing behind it is refused.

Talking to Google showed the layers working together, each relying on the one below it:

  • IP (layer 3) brought your packets to an address. That is why you first turned the name google.com into an address with ping.
  • TCP (layer 4) gave you a connection to port 80 at that address, exactly like the one between Bob and Alice. nc does not know what a web server is.
  • HTTP (layer 7) is what the browser and the web server say to each other over that connection. Google answered because what you typed was a valid request.

Your request had two lines. The first asked to GET the page at path /. The second, Host, named the site you wanted, although you had connected to an address: one address can serve many sites, so the request has to say which. The empty line after them, your second Enter, told the server that the request was complete. Its response had the same shape: a status line, headers, an empty line, then the page itself.

You could type the request because the version you used, HTTP/1.1, is text over TCP. Newer versions keep the same requests and responses but send them differently: HTTP/2 encodes them in binary, and HTTP/3 does not use TCP at all. Your browser uses them when the server supports them. The course uses HTTP/1.1 because you can read it, and what a request means has not changed.

Later in the course, your own application will listen on a port of your server and your browser will connect to it, as Alice did to Bob. And when a single server hosts several sites, the Host line is what decides which one answers.

🏛️ Architecture

This is a simplified architecture of the main running processes and communication flow during this exercise:

Two netcat processes connected over TCP Alice’s Server Bob’s Server Internet $>nc W.X.Y.Z 3000█ netcat nc W.X.Y.Z 3000 $>nc -l 3000█ netcat nc -l 3000 3000 Legend Process Communication Control Network Boundary Listening Port HelloWorld

💥 Troubleshooting

Here’s a few tips about some problems you may encounter during this exercise.

💥 400 Bad Request

If you get a 400 Bad Request response, it means that your HTTP request is invalid. You probably did not type exactly the text above.

💥 I don’t get a response

If you don’t get a response, it may be because you took too long to type the text, and the request has timed out. Try again a little faster.