Lab 4 Solution


Category: Tag:


In this lab you will extend Lab 3 to provide reliable file delivery using the Alternating Bit Protocol over UDP in Python 3. Packets will be dropped in both directions using one troll process on each machine. A copy of each transmitted data packet needs to be kept until it is ACKed. Note that as ACKs are not ACKed again, so the ACKs are unreliable. You can use a fixed retransmission timeout value of 50 ms.

Your implementation needs to work only for one file transfer. Observe that unlike in a traditional OS, you are implementing the transport layer reliability mechanism within the ap-plication layer itself. The file transfer protocol will include a server called and a client called In the following, we assume the client is running on and the server is running on First, start the server on using the command

python3 <local-port-on-gamma> <troll-port-on-gamma>

Then start troll on with the command (on one line)

troll -C <IP-address-of-beta> -S <IP-address-of-gamma> -a <client-port-on-beta> -b <server-port-on-gamma> <troll-port-on-beta> -t -x <packet-drop-%>

On, start troll with the command (on one line)

troll -C <IP-address-of-gamma> -S <IP-address-of-beta> -a <server-port-on-gamma> -b <client-port-on-beta> <troll-port-on-gamma> -t -x <packet-drop-%>

You will then start ftpc on with the command (on one line)

python3 <remote-IP-gamma> <remote-port-on-gamma> <troll-port-on-beta> <local-file-to-transfer>

The client will send all bytes of that local file to the troll process, which should forward the packets to The server should receive the file and then store it. Make sure that the new file created by is in a different directory to avoid overwriting the original file since all the CSE machines have your root directory mounted.

The file-transfer application uses a simple protocol. The payload of each UDP segment will contain the remote IP (4 bytes), remote port (2 bytes), a flag (1 byte), a 1-bit sequence number (1 byte) that can take values of 0 or 1, followed by a data/control field as explained below. The flag takes three possible values depending on the data/control field (explained on the next page):

  • In your lab, you can substitute any machine on stdlinux (e.g., or for and You should do so to avoid “overloading” these machines.


First segment (4 bytes): The first segment should contain the number of bytes in the file to follow (in network byte order). The flag is set to a value of 1.

Second segment (20 bytes): The second segment should contain 20 bytes which is the name of the file (assume the name can fit in 20 bytes). The flag is set to a value of 2.

Other segments: The other segments will contain data bytes from the file to be trans-

ferred. Each segment can have up to 1000 data bytes. The flag is set to a value of 3. Each ACK has 1 byte with a value of 0 or 1. The troll process will drop packets with the drop rate percentage <packet-drop-%> specified in the command line. Submit well-documented code using the following command:

submit c3461aa lab4 <code-directory-name>

Your program should work for binary files (images, etc.) of arbitrary size and the client and server should be running on different machines on stdlinux. You can use the sample images on the course webpage to test your program. Use diff or md5sum to ensure that the transferred file is the same as the original one. No buffer, array, or data structure used should exceed 1,200 bytes in size. You’ll need to “chunk” the file into “pieces,” each of which is at most 1,000 bytes. Submit a README.txt file with your lab. Please ensure that file transfer succeeds for any <packet-drop-%> value less than 20. The three types of processes will function in the following way:

troll: Each machine will run an instance of the troll program. troll runs continu-ously and processes each incoming packet. troll decides either to drop the packet or to forward it to its intended destination. sends one packet at a time. After sending the packet, it has to simulta-neously wait for two events: either a timeout or receipt of an ACK. Use the function for this purpose. (DO NOT use socket.settimeout() or other methods as they will not work correctly.) receives one packet at a time and after processing it, it takes the ap-propriate action (interpreting the size, interpreting the name of the file, and extracting the bytes of the file to write to the local disk). Upon receipt of a packet, it creates an appropri-ate ACK and sends it back.


error: Content is protected !!