// Algorithm for Receiver of Stop and Wait // This could be implemented as a thread or daemon // that is always running Inputs: packet_queue - this a queue where network layer gets packets received void Receiver(Queue packet_queue){ frame next_frame = new frame(), ack_frame = new frame(); int next_seq_no = 0; Packet next_p; long wait_time = 0; int result = 0; for (;;){ // wait until something arrives read_from_physical(frame); if (frame.get_type() != DATA_FRAME){ // PANIC!!! // Sender was supposed to send data at this point // not an ACK exit(1); } if (frame.get_seq() == next_seq_no){ // We got a the next expected frame // give packet to the network_layer next_p = frame.get_packet(); packet_queue.enqueue(next_p); // change the seq no to next expected value next_seq_no = (next_seq_no + 1) % 2; } // OK, now send ACK for packet received // notice that is we do not enter the if statement // above, it means we got a duplicate packet // So either our previous ACK was lost or it timed out }