CS2911
Network Protocols

This is an old version of this course, from Fall 2016. A newer version is available here.

Resources

You can work the entire lab as a prelab. Please work in teams of two unless approved by the instructor. Unlike Lab 2, please submit only one report per team. (You will comment which part of your team's work you did.)

Assignment

This week, you will submit your solution to the following problem:

You will write a program to receive a message over the network, using the fictitious next_byte function. You may write the received file to the screen or to a file.

A message has a four-byte header with a single field. This field contains the number of lines in a text file as a binary number. This four-byte field is in the standard network byte order, that is, big-endian.

Then, the lines follow, each terminated by '\n', that is, a new-line character.

For example, to send the text file

Lab 3
Phileus Fogg

This is my lab 3 assignment.

these bytes would be sent:

00 00 00 04 4c 61 62 20   33 0a 50 68 69 6c 65 75   ....Lab  3.Phileu
73 20 46 6f 67 67 0a 0a   54 68 69 73 20 69 73 20   s Fogg.. This is
6d 79 20 6c 61 62 20 33   20 61 73 73 69 67 6e 6d   my lab 3  assignm
65 6e 74 2e 0a                                      ent..

In this example, there are four lines of text (including the blank line). In the data, we see two \n's in a row (0a 0a) because one is the end of the line "Philues Fogg, and the next is the end of the blank line.

Suppose you can get one byte (single-byte string) of the message by calling next_byte(). There are no other ways to access the bytes in the message. There is no has_next_byte() method. Here's a full comment for next_byte:

# Read the next byte from the server sender.
# If the byte is not yet available, this method blocks (waits)
#   until the byte becomes available.
# If there are no more bytes, this method blocks indefinitely.
# Returns the next byte, as a string of one character. a bytes object with a single byte in it
def next_byte():

You do not need to implement the next_byte function. I will provide it in a later lab.

Your solution will be Python code, consisting of multiple methods. Put your top-level method first in the file. This should call other helper methods, which may call other helper methods as well.

Divide the coding effort as evenly as you can. In a comment above each method, include the author's name, with each team-member being the primary author for at least one method. Once you agree on who will write each method, be sure to agree on the arguments, return value, and purpose of each method. Include these in a comment at the beginning of the method. When you finish writing your code, revise the comments so they match the final version.

You do not need to run the code — it is OK if simple errors cause your code not to run at all. In fact, you really CAN'T run your code, since it relies on the next_byte() method, which is not available in Python, and which you don't have to write as part of your lab. You don't even need to import everything needed by your code.

I will consider these quality factors when grading this week's lab:

  • How well does the code break the large task into smaller sub-tasks?
  • Is the work divided reasonably among the team members?
  • Does the code read in the message in the specified format, without reading past the end of the message?
  • Does the code illustrate an understanding of how bytes and numbers are represented and converted in Python?
  • Does the code illustrate understanding of Python syntax, types, and binary encodings?

Example next_byte method

Although the "real" next_byte is not provided, you can use this example code to test your method. You can type in each byte of the message as hexadecimal shorthand, once for each call to the method. Or, you can write your own version...


# Enter the byte in hexadecimal shorthand
# e.g. 
#   input a byte: e3
#
# Returns the byte as a bytes object holding one byte
# 
# Author: Eric Nowac
def next_byte():
    msg = input("input a byte: ")
    return(int(msg, 16).to_bytes(1,'big'))