Java Reference
In-Depth Information
7.
This process should continue until the connection is lost. The client
and server are now communicating with each other in a simple
conversation.
The initial output of the server should be a String read from the client.
The initial output on the client should be a prompt to enter a message to
be sent to the server. When the client enters a message and presses Enter,
the message should be displayed on the server. Similarly, when a mes-
sage is entered on the server and the user presses Enter, the message
should be displayed on the client.
Lab 17.2: Using Datagram Packets
The purpose of this lab is to become familiar with sending and receiving
datagram packets. You will write a program that simulates a weather-
updating process, in which weather updates are sent to a listener every
15 seconds using datagram packets.
1. Write a class named WeatherUpdater that extends TimerTask. Add a
field of type DatagramSocket, a String field named recipientName,
and an int field named recipientPort. Add a constructor that takes in
a String and an int for these two fields, and also initialize the Data-
gramSocket object in the constructor using any available port.
2. Within the run() method of the WeatherUpdater class, create a
String that looks like “Current temp: 40”, where 40 is a randomly
generated number between 0 and 100 that changes each time the
method is invoked.
3. Within run(), instantiate a DatagramPacket appropriate for sending
a packet. The array of bytes should be the String in the previous step
converted to bytes. Send it to the recipient denoted by the recipient-
Name and recipientPort fields of the class.
4. Add main() to your WeatherUpdater class. Within main(), instanti-
ate a new WeatherUpdater object, using the first two command-line
arguments for the recipient's name and port number.
5. Within main(), instantiate a Timer object and use it to schedule the
WeatherUpdater with a fixed-rate schedule of every 15 seconds.
6. Save and compile the WeatherUpdater class.
Search WWH ::




Custom Search