2.implement programs to realize data communications, which will be running on different machines across networks, and using error control mechanisms.
3. The fundamental requirement of this project is to implement error control protocols by using Stop- And-Wait (SAW) and Go-Back-N (GBN) mechanisms, respectively.
4. It may include, but not limited to, maximum payload size, header size, number of fields, order and size of the different fields, different possible code values for certain fields, and message sequencing.
5. Your programs should be able to communicate with each other based on SAW to SAW and GBN to GBN.
6. Your program will use UDP segment as an underlying unreliable channel and put your protocol data unit (including header and payload) in the payload of the UDP segment.
7. To simulate the packet loss we will use pseudo random number to control that.
a. First your program will ask the user to input a number between [0, 99]. This number means the percentage of packet losses will happen statistically in the transmission.
b. Then each time before you send your protocol data unit, your program will generate a pseudo random number in the range [0, 99] with the random seed set to the current system time. If the random number generated is less than the user input number then the current protocol data unit won’t be sent (but assume it is sent) to simulate a packet loss.
8. To test your protocol, your program will read from a file named “DataSent.txt” and when received the data then put them into a file named “DataRecieved.txt”.
9. After your program finishes the transmission of the data file it should provide some basic statistic data including how long takes it to transmit the data file, how many total protocol data units sent, how many “lost” (simulated), etc.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a detailed implementation guide for the project you've described, incorporating both Stop-and-Wait (SAW) and Go-Back-N (GBN) error control mechanisms. Due to the complexity and the text-based nature of this platform, I'll provide a structured approach with pseudocode for key components. Actual implementation (e.g., in Python, C++, or Java) would require translating this guide into the chosen programming language.
Project Structure
SAW Sender
SAW Receiver
GBN Sender
GBN Receiver
Shared Utilities (for UDP handling, pseudo-random number generation, and file I/O)
Shared Utilities
Pseudocode for Shared Functions
markdown
# Function to generate a pseudo-random number to simulate packet loss
Function simulatePacketLoss(lossPercentage):
seedRandomWithCurrentTime()
randomNumber = generateRandomNumber(0, 99)
if randomNumber < lossPercentage:
return True
return False
# Function to send data via UDP (assumes setup of UDP socket)
Function sendViaUDP(socket, data, address):
if not simulatePacketLoss(packetLossPercentage):
# Simulate loss, do not send
print("Packet lost in transmission.")
return False
socket.sendto(data, address)
return True
# Function to receive data via UDP
Function receiveViaUDP(socket):
data, address = socket.recvfrom(bufferSize)
return data, address
# File I/O Functions (read from DataSent.txt, write to DataReceived.txt)
Function readFile(filename):
# Open file, read content, return as string or buffer
Function writeFile(filename, data):
# Open file, write data, close file
Stop-and-Wait (SAW) Implementation
SAW Sender
markdown
# Initialize UDP socket, set packetLossPercentage from user input
packetLossPercentage = getIntUserInput(0, 99)
socket = setupUDPSocket()
filename = "DataSent.txt"
fileContent = readFile(filename)
sequenceNumber = 0
maxPayloadSize = 1024 # Example value
timeout = 1 # Example timeout value
startTime = currentTime()
While there is more data to send:
# Prepare packet (header with sequence number + payload)
payload = getNextPayload(fileContent, maxPayloadSize)
packet = createPacket(sequenceNumber, payload)
While True:
if sendViaUDP(socket, packet, receiverAddress):
# Wait for ACK with timeout
ack, _ = receiveViaUDPWithTimeout(socket, timeout)
if ack and ack.sequenceNumber == sequenceNumber:
# Correct ACK received, move to next packet
sequenceNumber = 1 - sequenceNumber # Toggle for SAW
break
elif not ack:
# Timeout, resend packet
print("Timeout: Resending packet.")
else:
# Incorrect ACK, resend packet
print("Resending packet due to incorrect ACK.")
else:
# Packet 'lost', resend
print("Resending packet due to simulated loss.")
# Send final ACK to indicate end of transmission
finalPacket = createFinalPacket()
sendViaUDP(socket, finalPacket, receiverAddress)
# Calculate and display statistics
transmissionTime = currentTime() - startTime
print(f"Transmission Time: {transmissionTime} seconds")
print(f"Total Packets Sent: {totalPacketsSent}")
print(f"Packets Lost: {packetsLost}")
SAW Receiver
markdown
# Initialize UDP socket
socket = setupUDPSocket()
expectedSequenceNumber = 0
filename = "DataReceived.txt"
While True:
# Receive packet
packet, address = receiveViaUDP(socket)
if packet.sequenceNumber == expectedSequenceNumber:
# Correct packet, send ACK
ack = createACK(packet.sequenceNumber)
sendViaUDP(socket, ack, address)
# Write payload to file
writeFile(filename, packet.payload)
expectedSequenceNumber = 1 - expectedSequenceNumber # Toggle for SAW
else:
# Incorrect packet, send ACK for last correctly received packet
ack = createACK(1 - expectedSequenceNumber)
sendViaUDP(socket, ack, address)
# Check for end of transmission (e.g., special packet or timeout)
if isEndOfTransmission(packet):
break
Go-Back-N (GBN) Implementation
GBN Sender & Receiver will similarly utilize the shared utilities but with adjustments for windowing and cumulative ACKs. Due to the complexity and space constraints, here's a condensed overview:
GBN Sender
Key Differences from SAW:
Maintain a sliding window of unacknowledged packets.
Send packets in the window without waiting for individual ACKs.
Upon receiving an ACK, slide the window forward if the ACK is cumulative.
If a packet times out or an ACK indicates an error, Go-Back-N to the last acknowledged packet and resend all packets in the new window.
GBN Receiver
Key Differences from SAW:
Only send cumulative ACKs for correctly received, in-sequence packets.
If out-of-sequence packets are received, discard them and send the last cumulative ACK.
Statistics and Final Steps
After Transmission:
Calculate and display transmission time.
Count and display total protocol data units (PDUs) sent.
Count and display the number of 'lost' PDUs (simulated).
Optionally, display other relevant statistics (e.g., throughput, goodput).
Implementation in a Programming Language
To finalize this project, translate the pseudocode into your chosen programming language (e.g., Python for its simplicity with UDP via socket library, or C++/Java for more performance-critical applications). Ensure to handle potential errors, implement timeouts for GBN, and adjust buffer sizes as necessary for efficient data transfer.
Example Python Implementation for SAW:
python
import socket
import random
import time
# Shared Utility Functions
def simulate_packet_loss(loss_percentage):
return random.randint(0, 99) < loss_percentage
def send_via_udp(sock, data, address, loss_percentage):
if not simulate_packet_loss(loss_percentage):
print("Packet lost in transmission.")
return False
sock.sendto(data, address)
return True
def receive_via_udp(sock, timeout=1):
sock.settimeout(timeout)
try:
return sock.recvfrom(1024)
except socket.timeout:
return None, None
# SAW Sender Example
def saw_sender(loss_percentage, filename, receiver_addr):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
with open(filename, 'rb') as file:
data = file.read()
sequence_number = 0
start_time = time.time()
total_packets = 0
lost_packets = 0
for i in range(0, len(data), 1024):
payload = data[i:i+1024]
packet = sequence_number.to_bytes(1, 'big') + payload
total_packets += 1
while True:
if send_via_udp(sock, packet, receiver_addr, loss_percentage):
ack, _ = receive_via_udp(sock)
if ack and ack[0] == sequence_number.to_bytes(1, 'big'):
sequence_number = 1 - sequence_number
break
elif not ack:
lost_packets += 1
print("Timeout: Resending packet.")
else:
print("Resending packet due to incorrect ACK.")
else:
lost_packets += 1
print("Resending packet due to simulated loss.")
end_time = time.time()
print(f"Transmission Time: {end_time - start_time} seconds")
print(f"Total Packets Sent: {total_packets}")
print(f"Packets Lost: {lost_packets}")
# Example Usage
loss_percentage = 10 # 10% packet loss
filename = "DataSent.txt"
receiver_addr = ("localhost", 12345)
saw_sender(loss_percentage, filename, receiver_addr)
This Python example demonstrates the SAW protocol with packet loss simulation, timeouts, and basic statistics. The GBN implementation would require additional logic for window management and cumulative ACKs, which can be explored further in the code.