Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

Add code to the following class: Receiver to impose a four times timeout of 5 seconds. On the expiration of...

Add code to the following class: Receiver to impose a four times timeout of 5 seconds. On the expiration of first, second, and third timeout the following messages should be displayed on the console of the Receiver:

It is 5 seconds I am waiting.

It is 10 seconds I am waiting.

It is 15 seconds I am waiting.

On the expiration of fourth timeout the following message should be displayed on the console of the Receiver and the Receiver should be terminated:

It is 20 seconds I am waiting.

I waited 20 seconds to get your message. But you did not send it.

If the Sender sends its message during these time periods the Receiver should displays the message and terminates. The following are the result of five outputs of the Receiver:

Output 1: When the Sender sends its message before the expiration of 5 seconds:

Let us go to a movie

Output 2: When the Sender sends its message before the expiration of 10 seconds:

It is 5 seconds I am waiting.

Let us go to a movie

Output 3: When the Sender sends its message before the expiration of 15 seconds:

It is 5 seconds I am waiting.

It is 10 seconds I am waiting.

Let us go to a movie

Output 4: When the Sender sends its message before the expiration of 20 seconds:

It is 10 seconds I am waiting.

It is 15 seconds I am waiting.

Let us go to a movie

Output 5: When the Sender either sends its message after 20 seconds or does not send at all:

It is 5 seconds I am waiting.

It is 10 seconds I am waiting.

It is 15 seconds I am waiting.

It is 20 seconds I am waiting.

I waited 20 seconds to get your message. I do not wait anymore.

Note: This question is similar (not the same) as exercise 4 in chapter 4 of your book.

Note: Only copy/paste the class: Receiver class under the word: Answer.

Hints:

1.      You need to call the java method: setSoTimeout(5000) of class: DatagramSocket.

2.      You need an extra catch-block with the exception class: SocketTimeoutException.

3.      You need a loop that iterates at most four times every 5 seconds.

Sender:

import java.io.*;

import java.net.*;

publicclass Sender {

  publicstaticvoid main(String[] args) throws IOException {

    // This class is complete, do not change it. 

    try {

      String message = "Let us go to a movie";

      byte[] buffer = message.getBytes();

      intport = 16790;

      InetAddress host = InetAddress.getByName("localhost");

      DatagramSocket serverSocket = new DatagramSocket();

      DatagramPacket datagram = new DatagramPacket(buffer, buffer.length, host, port);

      serverSocket.send(datagram);

      serverSocket.close();

    } catch (IOException e) {

        System.out.println("Error: " + e);

        System.exit(0);

    }

  }

}

Receiver:

import java.io.*;

import java.net.*;

publicclass Receiver {

  // Complete this class.

  publicstaticvoid main(String[] args) throws IOException {

    finalintMAX_LEN = 100;

    byte[] buffer = newbyte[MAX_LEN];

    intport = 16790;

    DatagramPacket datagram = new DatagramPacket(buffer, buffer.length);

    DatagramSocket clientSocket = new DatagramSocket(port);

    .......

  }

}

Answer:

Receiver:

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question