Socket Programming

For this reference page, we refer to the lab1 you’re building as the client and the web server it’s connecting to as the server.

As we’ll see in class, a protocol defines both the message & header format and transfer procedure. With HTTP, your lab code must first establish a connection before it starts sending and receiving data.

To establish this connection, the application layer (your lab1 web client), relies on a transport layer protocol known as TCP. TCP offers in-order, reliable delivery, making it a common transport protocol choice. To start a TCP connection, your client will create a socket. You can think of a socket like a mailbox to send and receive messages.

Socket Programming: the Socket interface sits between the Application layer and the transport layer. We associate the client and and server with a socket to communicate across the network. The routers in-between do not "speak" the transport layer or application layer protocols. This is analogous to not having the postal mail system look inside your mail!
Figure 1. The Socket interface sits between the Application layer and the transport layer. We associate the client and server with a socket to communicate across the network. The routers in between do not "speak" the transport layer or application layer protocols. This is analogous to not having the postal mail system look inside your mail!

Socket system-calls

First you need to create a socket on the client side. Once you have a socket, you’ll need to connect it to the server, which requires you to specify the server’s address using a struct sockaddr_in (details in the next section). After your socket is connected, you can send and receive data. Eventually, you’ll need to close the socket, after you’re done using it.

  • socket(): create a new communication endpoint

  • connect(): actively attempt to establish a connection

  • send(): send some data over a connection

  • recv(): receive some data over a connection

  • close(): close the connection.

Look through the system calls provided in lab1.c and their comments.

Resolving a hostname to an IP address

Before you can send an HTTP GET message to the server, you need to translate the destination’s hostname (demo.cs.swarthmore.edu) to its corresponding IP address. Fortunately, there’s a system that will do this for you: the domain name system (DNS). We’ll study DNS in much more detail later, so we’re going to treat it like a black box that magically converts hostnames to IP address in lab 1. For now, you need to know that there are two functions that will translate hostnames to IP address:

  1. The gethostbyname function is the old interface. You shouldn’t use it for this lab, but you should know of its existence, as you may encounter it some day — it’ll probably never die out for good. I’ve provided an example in gethostbyname.c. You may want to look over it briefly, but don’t spend too much time on it.

  2. The getaddrinfo function is the preferred function for new code. To use it, you’ll pass in the hostname, and it will fill in a pointer to a struct addrinfo struct. On failure, that pointer will be NULL, and you shouldn’t attempt to use it. On success, it’ll point to a struct that contains the answer you’re looking for. After you’re done using it, you should free the struct with freeaddrinfo. See the getaddrinfo.c example for details.