Home CPSC 414

Lab 3: Server Sockets

 

Objective

To practice writing programs which use sockets and work with the client/server network model.


 

Opening a Port

For this lab, you will need to run a server on your Google cloud VM. By default, Google blocks communication on ports other than a select few (including 22 for SSH, and 80 for the web if you select it).

So in order to create a server, we must first open a port in the firewall. A firewall is a system that monitors network traffic coming into or out of a machine. It has a list of rules on what sort of traffic is allowed. If we don't tell the firewall to allow traffic on a port, none will get through.

To do this, follow these steps:

  1. Navigate to http://console.cloud.google.com/.
  2. Click on "Compute Engine" under the "Resources" tab.
  3. Click the "⋮" next to your VM, and select "View network details".
  4. Choose "Firewall rules" from the menu on the left.
  5. Click "Create Firewall Rule" from the top of the screen.
  6. For name put anything you like. Maybe "lab3".
  7. Leave "Logs" off, and the Network on "default".
  8. Leave "Direction of traffic" on "Ingress", and "Action on match" as "Allow".
  9. Change "Targets" to "All instances in the network".
  10. Set "Source filter" to "IP Ranges".
  11. Put in "0.0.0.0/0" for "Source IP Ranges" which will allow any IP to connect on the port.
  12. Leave "Second source filter" on "None"
  13. Set "Protocols and ports" to "Specified protocols and ports".
  14. Check "TCP", and put the port you want into the field next to it. For this lab, we can use 4040.
  15. Finally, click "Create".

If everything was filled out, you should be back at the list of firewall rules. You should see a row for the rule we just added, like this:


Our firewall rule


 

Task

Once we have the port open, we can run a server which is listening on this port. For this lab you'll make a "time server". This server will listen on port 4040 for connections. Each time a client connects, it will send out the current date and time to the client, and then wait for the next client.


 

Details

  1. Start with the caps-server.py example we saw in class.
  2. Change the host that is passed into bind to the internal IP address of your VM.
  3. Change the port to the one that you opened, 4040.
  4. Instead of the code to read a value, capitalize it, and resend it, put in code to send the client the current day and time. In Python, we can import the datetime library with an import statement, and then use the following to get this as a string:
    
    datetime.datetime.now().strftime("%A, %B %d, %Y, %I:%M %p\n")
    
    
  5. After sending the data, the server should close the connection.
  6. Put the call to accept and the sending of the date and time inside of an infinite loop. This way, the server will accept all of the clients that connect, one after the other, rather than just one. An infinite loop in Python looks like this:
    
    while True:
        # body of the loop
    
    

 

Testing

Because this is such a simple server, we do not really need a whole client to test it. Instead we can use the very handy program called telnet.

telnet is sort of like a generic client, you can connect it to any server and port, and send and receive data back and forth from it. Telnet used to come with all major computer systems.

If you're using Windows, you have telnet, but need to enable it following these instructions. If you're using a Mac, you need to install it following this guide instead. If you are using Linux, you already have telnet.

To test that telnet itself is installed, you connect to the server towel.blinkenlights.nl, which will show you an ASCII-art version of Star Wars:

$ telnet towel.blinkenlights.nl

You can quit telnet with Control-]. Then type "quit" at the telnet prompt.

To test your time and date server, just connect telnet to the external IP of your VM, and the port 4040. It should print the date and time and return:

$ telnet 34.73.15.22 4040
Trying 34.73.15.22...
Connected to 34.73.15.22.
Escape character is '^]'.
The day and time is: Thursday, January 31, 2019, 12:32 AM
Connection closed by foreign host.

If you see something like this, then you have done it correctly! Note that your VM may not be in the same time zone as us, so the time may be a few hours off. That's OK.

Make sure that you can connect over and over again and that the server will continue to respond to you.


 

Submitting

When you're finished, email your server .py file to ifinlay@umw.edu.

Copyright © 2024 Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.