Lab 3 CN

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

HARSH KUMAR

2100290110059
COMPUTER NETWORK A2 BATCH
COMPUTER NETWORKS EXPERIMENT LAB 3

Implement the simplex and stop and wait protocols with any
programming language.

The Stop-and-Wait protocol between a sender and a receiver. The sender


sends frames one at a time and waits for an acknowledgment (ACK) from the
receiver before sending the next frame. If the sender does not receive an ACK
for a frame, it resends the same frame.

You can run this code to see how the sender sends frames and the receiver
acknowledges them in a stop-and-wait manner.

For the Simplex protocol, the implementation would depend on whether


implementing simplex communication over a simplex medium (one-way
communication) or using simplex as part of a full-duplex system (where
communication can happen in both directions, but only one direction at a
time).
HARSH KUMAR
2100290110059
COMPUTER NETWORK A2 BATCH
HARSH KUMAR
2100290110059
COMPUTER NETWORK A2 BATCH

CODE:
import time

def sender(message):

seq_num = 0

while True:

print("Sender: Sending frame with sequence number", seq_num)

time.sleep(1) # Simulating transmission time

print("Receiver: Receiving frame...")

ack = receiver(seq_num)

if ack == "ACK":
HARSH KUMAR
2100290110059
COMPUTER NETWORK A2 BATCH
print("Sender: Frame", seq_num, "successfully received by receiver")

seq_num = (seq_num + 1) % 2 # Toggle sequence number (0 or 1)

if message:

print("Sender: Waiting for next frame...")

else:

print("Sender: All frames sent successfully")

break

else:

print("Sender: Frame", seq_num, "not received correctly, resending...")

def receiver(expected_seq_num):

time.sleep(1) # Simulating transmission time

print("Receiver: Sending ACK for frame with sequence number", expected_seq_num)

return "ACK" # Simulating positive acknowledgment

# Example usage:

message = [1, 2, 3, 4, 5]

sender(message)

OUTPUT:
Sender: Sending frame with sequence number 0

Receiver: Receiving frame...

Receiver: Sending ACK for frame with sequence number 0

Sender: Frame 0 successfully received by receiver

Sender: Waiting for next frame...

Sender: Sending frame with sequence number 1

Receiver: Receiving frame...

Receiver: Sending ACK for frame with sequence number 1

Sender: Frame 1 successfully received by receiver

Sender: Waiting for next frame...


HARSH KUMAR
2100290110059
COMPUTER NETWORK A2 BATCH
Sender: Sending frame with sequence number 0

Receiver: Receiving frame...

Receiver: Sending ACK for frame with sequence number 0

Sender: Frame 0 successfully received by receiver

Sender: Waiting for next frame...

Sender: Sending frame with sequence number 1

Receiver: Receiving frame...

Receiver: Sending ACK for frame with sequence number 1

Sender: Frame 1 successfully received by receiver

Sender: Waiting for next frame...

Sender: Sending frame with sequence number 0

Receiver: Receiving frame...

Receiver: Sending ACK for frame with sequence number 0

Sender: Frame 0 successfully received by receiver

Sender: Waiting for next frame...

Sender: Sending frame with sequence number 1

Receiver: Receiving frame...

Receiver: Sending ACK for frame with sequence number 1

Sender: Frame 1 successfully received by receiver

Sender: All frames sent successfully

This output demonstrates the stop-and-wait protocol in action. The sender sends frames with
sequence numbers 0 and 1 alternately, and the receiver acknowledges each frame with the
corresponding sequence number. If the sender does not receive an acknowledgment, it resends
the frame.

You might also like