Question

I am implementing 2 applications that use the sliding window protocol as a data link protocol and UDP Sockets to communicate. I am trying to implement the sliding window protocol using the Tanembaum's book as reference. Here's the code I found in the book.

/* Protocol 4 (sliding window) is bidirectional and is more robust than protocol 3. */

    #define MAX_SEQ 1   /* must be 1 for protocol 4 */
    typedef enum {frame_arrival, cksum_err, timeout} event_type;
    #include "protocol.h"

    void protocol4 (void)
    {
    seq_nr next_frame_to_send;  /* 0 or 1 only */
    seq_nr frame_expected;  /* 0 or 1 only */
    frame r, s; /* scratch variables */
    packet buffer;  /* current packet being sent */
    event_type event;

    next_frame_to_send = 0; /* next frame on the outbound stream */
    frame_expected = 0; /* number of frame arriving frame expected */
    from_network_layer(&buffer);    /* fetch a packet from the network layer */
    s.info = buffer;    /* prepare to send the initial frame */
    s.seq = next_frame_to_send; /* insert sequence number into frame */
    s.ack = 1 - frame_expected; /* piggybacked ack */
    to_physical_layer(&s);  /* transmit the frame */
    start_timer(s.seq); /* start the timer running */

    while (true) {
    wait_for_event(&event); /* could be: frame_arrival, cksum_err, timeout */
    if (event == frame_arrival) { /* a frame has arrived undamaged. */
            from_physical_layer(&r);    /* go get it */

            if (r.seq == frame_expected) {
                    /* Handle inbound frame stream. */
                    to_network_layer(&r.info);  /* pass packet to network layer */
                    inc(frame_expected);    /* invert sequence number expected next */
            }

            if (r.ack == next_frame_to_send) { /* handle outbound frame stream. */
                    from_network_layer(&buffer);    /* fetch new packet from network layer */
                    inc(next_frame_to_send);    /* invert sender's sequence number */
            }
    }

    s.info = buffer;    /* construct outbound frame */
    s.seq = next_frame_to_send; /* insert sequence number into it */
    s.ack = 1 - frame_expected; /* seq number of last received frame */
    to_physical_layer(&s);  /* transmit a frame */
    start_timer(s.seq); /* start the timer running */
  }
  }

I am a little confused in translating this to java. I don't know exactly how threads are applied here. I am looking for any hints and strategies that will help me implementing this.

EDIT

I'm most confused :

  • in implementing the functions wait_for_event() and start_timer().
  • Where should I put the UDP sockets part? I think the UDP socket part is within to_physical_layer() and from_physical_layer(). Correct if I'm wrong.
  • Should both application have the same code since the protocol is bidirectional?
Was it helpful?

Solution

in implementing the functions wait_for_event() and start_timer().

You don't need them. Use DatagramSocket.setSoTimeout() and catch (SocketTimeoutException) respectively.

Where should I put the UDP sockets part? I think the UDP socket part is within to_physical_layer() and from_physical_layer(). Correct if I'm wrong.

Same place as the present recv() and send() calls.

Should both application have the same code since the protocol is bidirectional?

Yes.

OTHER TIPS

First of all: before investing time and effort into a sliding-window layer on top of UDP, check whether you need such an exotic solution. TCP already does that kind of stuff, and many people have invested lots of time in getting it right and robust. But there are of course valid reasons to implement such a solution. The next step should be to google around, chances are there are already dozens of very similar programs around. Especially because you don't seem to have a lot of experience with Java's networking API's it's probably a whole lot more efficient to start from a working piece of Java code than from an example from a book that's more geared toward learning people networking concepts.

Anyways, if you want to reimplement the protocol above start by reading the Javadoc for the DatagramSocket class. It has a setSoTimeout method that can replace the timer thing in the code above, the main difference being that upon timeout receive will throw a SocketTimeoutException, and that may complicate your program's flow a little. Forget the from_physical and to_physical bits, all that stuff is wrapped by the Socket classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top