CSc 423 Assignment 6

The Last Time

Assigned
Due

Apr 11
40 pts
Apr 30

RFC 867 defines the very simple daytime protocol. So we need to write a server for it. Here is an example of a server written with Cleansocks. Like it, your server should should take an optional port number on the command line, or use the standard daytime server port otherwise. (The port number is 13, but don't hard-code it, look it up as usual.) As we know, the service is very simple. Your server should wait for connections on the specified port. When a client connects, do not read anything from the client, simply send a single line of text containing the date and time, terminated with \r\n, then close the connection.

Since RFC 867 does not specify the format of the time string, you are free to send anything reasonable. You should read the time from the system clock and format it as a string. Use the standard Unix/C time function to get the time, and some reasonable combination of formatting functions, ctime and friends and/or strftime to generate an appropriate string. (Alternatively, you may use the much newer C++ chrono facility. Seems like more work.)

Your program should:

  1. Get the port number from the command line, or use the standard daytime port.
  2. Create a port listening on that socket number, on any network interface.
  3. Enter a loop serving inbound connections. For each one:
    • Accept the connection.
    • Build a string of the current date and time.
    • Write the the string to the client.
    • Close the connection.

Note that your server is an infinite loop. It ends when you kill it with control-c. In addition, I printed a message to the console when a client connects. That's useful to see what's going on. Generally, though, a server doesn't have much to say on the console. It does its talking on the network.

Running Your Program

The easiest way to test your program is using localhost and two command windows. You can use netcat as a client, or assignment one might work just as well. (At least it will only choose timely quotations.) For instance, in one window, I run my server, and it looks like this:
[bennet@bennet asst]$ ./daytimeserv 6623 Accepted connection from 127.0.0.1:43198 Accepted connection from 127.0.0.1:59284 Accepted connection from 127.0.0.1:58184 Accepted connection from 127.0.0.1:35438 ^C
because in another window, I'm doing this:
[bennet@bennet asst]$ nc --recv-only localhost 6623 Thu Apr 11 18:39:49 2024 [bennet@bennet asst]$ nc --recv-only localhost 6623 Thu Apr 11 18:40:24 2024 [bennet@bennet asst]$ nc --recv-only localhost 6623 Thu Apr 11 18:40:33 2024 [bennet@bennet asst]$ nc --recv-only localhost 6623 Thu Apr 11 18:40:43 2024 [bennet@bennet asst]$
Keep in mind that my only keyboard interaction with the server is to run it, then kill it when I'm done. Most of the typing is in the other window.

I used a port number other than the standard one because, on a Unix system, only the administrator can run servers on ports 1-1024. This was a bit of a security precaution from back when computers were expensive, and were never owned by one person. I don't know if Windows supports this restriction. The 127.0.0.1 in the server messages is the IP address of localhost.

To use your server over an actual Internet connection, you may find yourself fighting firewalls. If you have multiple machines at home, you can run your server on one and client on another. You may need to open the port you're using in the firewall on the server machine. A Windows box may offer to do that for you when you run the server and it notices the listen. Alternatively, if you run your server on Sandbox, it happens that ports 45000-45500 are open for occasional use by FTP data connections. You might find you can run your server somewhere in that port range and connect to it remotely.

In case you are interested, this protocol is no longer widely used, but there are a few public servers available:
[bennet@bennet asst]$ nc --recv-only time.nist.gov 13 60411 24-04-11 23:49:46 50 0 0 934.3 UTC(NIST) *
Which also emphasizes the fact that the RFC does not specify the format of the string returned.

Submission

When your program is working, nicely commented and properly indented, submit it using the form here.