I will offer more points to who ever answers this for me. The purpose of this la
ID: 3641881 • Letter: I
Question
I will offer more points to who ever answers this for me.
The purpose of this lab is to become familiar with sending and receiving
datagram packets. You will write a program that simulates a weatherupdating 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 DatagramSocket 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 recipientName and recipientPort fields of the class.
4. Add main() to your WeatherUpdater class. Within main(), instantiate 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.
7. Write a class named WeatherWatcher that contains main().
8. Within main(), instantiate a DatagramSocket on port 4444. Also,
instantiate a DatagramPacket suitable for receipt, by using an array
of bytes of size 128.
9. Invoke the receive() method within main() so that the thread blocks
until a datagram packet is delivered.
10. After a packet is delivered, print out its contents. Create a loop so
that the WeatherWatcher then invokes receive() again, to be ready
for the next packet to be delivered.
11. Save, compile, and run the WeatherWatcher program.
12. Run the WeatherUpdater program, passing in the appropriate
host name (localhost if both are on the same computer) and port
number 4444.
The WeatherUpdater probably will not display any output. However,
in the WeatherWatcher program, you should see the statement “Current
temp: N” every 15 seconds or so, with the temperature changing randomly
Explanation / Answer
please rate. WeatherUpdater.java =============================================================================== import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Random; import java.util.Timer; import java.util.TimerTask; public class WeatherUpdater extends TimerTask{ DatagramSocket socket; String recipientName; int recipientPort; public WeatherUpdater(String recipientName,int recipientPort) throws SocketException{ this.recipientName = recipientName; this.recipientPort = recipientPort; socket = new DatagramSocket(); } @Override public void run() { Random rand = new Random(); int temperature = rand.nextInt(100); String temp = "Current temperature is "+temperature; InetAddress ipaddress = null; try { ipaddress = InetAddress.getByName(recipientName); } catch (UnknownHostException e) { System.out.println("No such recipient available. "+e.getMessage()); return; } DatagramPacket packet = new DatagramPacket(temp.getBytes(),temp.length(),ipaddress,recipientPort); try { socket.send(packet); } catch (IOException e) { System.out.println("Unable to send packet. "+e.getMessage()); } } public static void main(String args[]){ if(args.length!=2){ System.out.println("Usage: java WeatherUpdater [recepient name] [recepient port]"); return; } WeatherUpdater updater = null; String recipientName = args[0]; int recipientPort = Integer.parseInt(args[1]); try { updater = new WeatherUpdater(recipientName, recipientPort); System.out.println("Starting weather updater service."); Timer timer = new Timer("Weather updater timer"); int delay = 0; int period = 15000; timer.schedule(updater,delay,period); } catch (SocketException e) { System.out.println("Unable to create weather updater. "+e.getMessage()); } } } ============================================================================= WeatherWatcher.java import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class WeatherWatcher { public static void main(String args[]){ DatagramSocket socket = null; try { socket = new DatagramSocket(4444); } catch (SocketException e) { System.out.println("Unable to create the desired socket. "+e.getMessage()); } int size = 128; byte[] b = new byte[size]; DatagramPacket packet = new DatagramPacket(b,size); System.out.println("Starting weather watcher. "); while(true){ try { socket.receive(packet); } catch (IOException e) { System.out.println("Unable to receive packet. "+e.getMessage()); } String temperature = new String(b); System.out.println(temperature); } } } ============================================================================= First run WeatherWatcher.java next run WeatherUpdater.java with arguments localhost 4444