CS 321 Spring 2013  >  Lecture Notes for Friday, April 12, 2013

CS 321 Spring 2013
Lecture Notes for Friday, April 12, 2013


A Berkeley socket (usually just socket) is a standard interface to a network connection. Sockets were introduced in the Berkeley version of Unix; they are now found in all major OSs.

Sockets can be used to handle any networking protocol; we will use sockets to do TCP. Rather than deal with the standard interface directly, I will use a library written by Dr. Lawlor: osl/socket.h and osl/socket.cpp. These are on the webpage; they are also available under NetRun.

A socket represents a network connection. We request a socket attached to a specified port. If the port is in use or is not available at our privilege level, then the operation fails. If the operation succeeds and we acquire the socket, then only this process has access to the port until the socket is released.

The above process is used when we are the one initiating the conversation (e.g., a web browser). If we are at the other end (e.g., a web server), then we obtain a server socket, which we use to listen on a port. As before, the port is reserved, if possible. When a message is received, we obtain a socket that wraps the connection to the sender. Multiple sockets may be associated with a single server socket, allowing a single port to be used for multiple concurrent conversations.

When we use our sockets, we provide only the body of TCP segments; the header is constructed for us.

See the various client and server programs on the webpage for examples of code that uses sockets.

CS 321 Spring 2013: Lecture Notes for Friday, April 12, 2013 / Updated: 6 May 2013 / Glenn G. Chappell / ggchappell@alaska.edu