13 Spring21
13 Spring21
1
Version 1
Accessing Code Examples
● Today's lecture examples reside within:
/usr/class/cs110/lecture-examples/networking.
○ First ssh into a myth machine (ssh [email protected]). When prompted
for your password, it is normal for the text not to appear as you enter your password.
Once logged onto a myth machine, cd into the above directory.
○ To get started, type:
git clone /usr/class/cs110/lecture-examples cs110-lecture-examples
at the command prompt to create a local copy of the master.
○ Each time I mention there are new examples (or whenever you think to), descend into
your local copy and type git pull. Doing so will update your local copy to match
whatever the master has become.
Server-side applications set up a socket that listens on a particular port. From the perspective of the Linux kernel, a socket is
an endpoint of a connection. But to a Linux program, a socket is an open file with a corresponding descriptor.
● A port number is like a virtual process ID that the host associates with the true pid of the application’s process.
● Each socket has a socket address that consists of an internet address and a 16-bit integer port. The socket address is
denoted by the notation address:port.
● The port in the client’s socket address is assigned automatically by the kernel when the client makes a connection
request and this port is known as an ephemeral port.
● By contrast, the port in the server’s socket address is usually some well-known port that is permanently associated with
the service (example: http, the well-known service name for the Web service, is port 80; https is 443).
● A connection is uniquely identified by a socket pair: the socket addresses of its two end points.
○ The socket pair is denoted by the tuple (client address:client port, server address:server port).
Roslyn Michelle Cyrus | Stanford University 7
Reading: B&O’s Network Programming chapter
Networking
● You can see some of the ports your computer is listening to with the netstat command:
● Some common ports are listed above. You can see a full list here and here.
○ Ports 25 and 587 are SMTP (Simple Mail Transfer Protocol), for sending and receiving email.
○ Port 53 is the DNS (Domain Name Service) port, for associating names with IP addresses.
○ Port 22 is the port for SSH (Secure Shell)
○ Port 631 is for IPP (internet printing protocol)
● For your own programs, generally try to stay away from port numbers listed in the links above, but otherwise, ports
are up for grabs to any program that wants one.
● accept (found in sys/socket.h) returns a descriptor that can be written to and read from. Whatever's
written is sent to the client, and whatever the client sends back is readable here.
○ This descriptor is one end of a bidirectional pipe bridging two processes—even if they are on
different machines!
● The first five lines here produce the full time string that should be published.
○ Let these five lines represent more generally the server-side computation needed for the service to
produce output. Here, the payload is the current time, but it could have been a static HTML page, a
Google search result, an RSS document, or a movie on Netflix.
● The remaining lines publish the time string to the client socket using the low-level I/O we've seen before.
Roslyn Michelle Cyrus | Stanford University 16
Reading: B&O’s Network Programming chapter
Networking
● Note that the while loop for writing bytes is a bit more important now that we are networking: we are more
likely to need to write multiple times on a network.
○ The socket descriptor is bound to a network driver that may have a limited amount of space.
○ That means write's return value could very well be less than what was supplied by the third
argument.
● Ideally, we'd rely on either C streams (e.g. the FILE *) or C++ streams (e.g. the iostream class hierarchy) to
layer over data buffers and manage the while loop around exposed write calls for us.
● Fortunately, there's a stable, easy-to-use third-party library—one called socket++ that provides exactly this.
○ socket++ provides iostream subclasses that respond to operator<<, operator>>, getline, endl,
and so forth, just like cin, cout, and file streams do.
○ We are going to operate as if this third-party library was just part of standard C++.
● The next slide shows a prettier version of publishTime.
● We'll soon discuss the implementation of createClientSocket. For now, view it as a built-in that sets up
a bidirectional pipe between a client and a server running on the specified host (e.g. myth61) and bound
to the specified port number (e.g. 12345).
Roslyn Michelle Cyrus | Stanford University 22
Reading: B&O’s Network Programming chapter
Networking
● Here’s some output when running our server and client (in this example, on the same machine):
24
Roslyn Michelle Cyrus | Stanford University
Reading: B&O’s Network Programming chapter
Networking
● Yes! See the following example:
On myth61:
rcyrus@myth61$ ./time-server-concurrent
rcyrus@myth61$ Server listening on port 12345.
On another myth:
rcyrus@myth66$ ./time-client myth61.stanford.edu 12345
Fri May 14 20:49:51 2021
rcyrus@myth66$ ./time-client myth61.stanford.edu 12345
Fri May 14 20:50:42 2021
rcyrus@myth66$ ./time-client myth61.stanford.edu 12345
Fri May 14 20:50:43 2021
rcyrus@myth66$ ./time-client myth61.stanford.edu 12345
Fri May 14 20:50:44 2021
rcyrus@myth66$
26
Roslyn Michelle Cyrus | Stanford University