Lab 12: select
Due: Electronically, by 11:59 PM on Wednesday December 9 Notice that this is an extension since this lab was posted late.
Introduction
The purpose of this lab is to practice using the select
system call.
Procedure Review
You should submit your work on MarkUs so begin by checking out your repo with the new lab12 directory.
Copy the files from /u/csc209h/fall/pub/lab12
to your lab12
directory. Use the makefile
to compile the programs.
Choosing a Port
The port on which the server listens is defined on the second line of the makefile. I have it set to 30001 by default; before continuing I’d like you to change this.
To avoid port conflicts, I’m asking you to use the following number as the port on which your server will listen: take the last four digits of your student number (or your partner’s student number), and add a 5 in front. For example, if your student number is 998123456, your port would be 53456. Using this base port, you may add 1 to it as necessary in order to use new ports (for example, the fictitious student here could also use 53457, 53458, 53459, 53460). When you shutdown your server (e.g. to compile and run it again), the OS will not release the old port immediately, so you may have to cycle through ports a bit.
Remember that the -D
flag to gcc
lets you define a constant at the commandline.
Server and Client
You are given a completed server (countdown.c
). It accepts a connection from one client and prints a countdown message to that client once every 2 seconds. When the counter reaches 0, the server exits. The server also reads an integer from the client, and uses that integer to change the value of the counter. In this way, the client can prevent the counter from reaching 0 or speed up the descent to 0.
Notice that the server uses the timeout argument of select
to implement the countdown. The server is waiting for data on only one file descriptor, but without select
there is no way to make a blocking read
call timeout.
The client (client.c
) reads a number (as a string) from standard input and writes it to the server as an int
(not as a string). It will also read values from the server and print them to the screen so that the user can see the value of the server’s counter as it changes.
You will be adding code to complete client.c
.
Other Starter Files
In addition to countdown.c
and client.c
, you are also given some files containing system call wrappers. wrapsock.c
contains error-checking versions of the standard socket functions. You can call these without worrying about errors: if they fail, they’ll perror
and exit
automatically.
Modifying the Client
You will see in the starter code that all of the socket setup has been done for you in both the server and client. Your task is to complete the client so that it uses select
to monitor both the socket and standard input. When an integer is received from the server, it should be printed. When an integer is received from standard input, it should be sent to the server. Check the end of client.c
for the output statements you should use in your code.
Example Client Output
Here is an example of client output:
PORT = 30001 Enter a number: Counter is 9 Enter a number: <user types 4> Enter a number: Counter is 3 Enter a number: Counter is 2 Enter a number: <user types 4> Enter a number: Counter is 3 Enter a number: Counter is 2 Enter a number: Counter is 1 Enter a number: Counter is 0 Enter a number: Server Terminated, closing connection
Submitting
Submit client.c
.