HTTP request with netcat

We are now ready to use nc.

Launch a terminal:

On Windows (assuming you've completed our guide):

  1. Press WinKey+R
  2. Type cmder
  3. Press Enter

On macOS:

  1. Launch Spotlight (Cmd+Space by default)
  2. Type Terminal.app
  3. Press Enter

Now, type

nc 127.0.0.1 8080

We've put three things separated by spaces:

  1. The name of the app.
  2. First parameter that nc requires: the address of the machine we want to connect to.
  3. Second parameter that nc requires: the port.

127.0.0.1 is a special IP-address that corresponds to "this computer". It's like a pointer that always points to the current place: if I run a program on my laptop, then 127.0.0.1 means "my laptop". If I run the same program on my mom's PC, 127.0.0.1 will mean "my mom's PC". We use this address here because we will run both the client app and the server app on the same physical computer.

I know we talked about things as if the client and the server must be located on different computers, but hey, these are just apps, and we can certainly launch multiple apps at the same time on a single machine.

The second parameter for nc is a port. There can be multiple different server applications running on a single machine. In addition to a web server, we could run a database server, a chat server, an email server and any number of other usual or unusual servers. All of them communicate with the world via the Internet, but how can we make sure that HTTP requests go to the web server, but not to the database server?

This is similar to having an apartment building: if a postal courier comes with a package for Mr Jekyll, they must know the apartment number. The computer is like that building, and different apps are running behind different doors or ports. In our example above we put 8080 as port number. Our assumption is that the web-server is indeed running behind door 8080.

By the way, port literally means "door" in some languages.

When you hit Enter after typing nc 127.0.0.1 8080, nothing will happen. Because there is no app currently running on the same machine listening for connecting on port 8080.

XIt's time to build this app.