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:
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Writedemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Place Order” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.