Chapter 11. Sockets and Networking: There’s no place like 127.0.0.1

image with no caption

Programs on different machines need to talk to each other.

You’ve learned how to use I/O to communicate with files and how processes on the same machine can communicate with each other. Now you’re going to reach out to the rest of the world, and learn how to write C programs that can talk to other programs across the network and across the world. By the end of this chapter, you’ll be able to create programs that behave as servers and programs that behave as clients.

The Internet knock-knock server

C is used to write most of the low-level networking code on the Internet. Most networked applications need two separate programs: a server and a client.

You’re going to build a server in C that tells jokes over the Internet. You’ll be able to start the server on one machine like this:

image with no caption

Other than telling you it’s running, the server won’t display anything else on the screen. However, if you open a second console, you’ll be able to connect to the server using a client program called telnet. Telnet takes two parameters: the address of the server, and the port the server is running on. If you are running telnet on the same machine as the server, you can use 127.0.0.1 for the address:

Watch it!

You’ll be using ...

Get Head First C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.