Question: Modify the original Example1*.java code so that the receiver loops indefinitely to receive and output datagrams. Recompile. Then (i) start the receiver, (ii) execute the

  1. Modify the original Example1*.java code so that the receiver loops indefinitely to receive and output datagrams. Recompile. Then (i) start the receiver, (ii) execute the sender, sending a message message 1, (iii) in another window, start another instance of the sender, sending a message message 2. Does the receiver receive both messages? Capture the output.
    1. Example1Receiver 10000
    2. Example1Sender localhost 10000 hello world

Example 1Sender

import java.net.*; import java.io.*;

/** * This example illustrates the basic method calls for connectionless * datagram socket. * @author M. L. Liu */ public class Example1Sender {

// An application which sends a message using connectionless // datagram socket. // Three command line arguments are expected, in order: // // //

public static void main(String[] args) { if (args.length != 3) System.out.println ("This program requires three command line arguments"); else { try { InetAddress receiverHost = InetAddress.getByName(args[0]); int receiverPort = Integer.parseInt(args[1]); String message = args[2]; DatagramSocket mySocket = new DatagramSocket(); // instantiates a datagram socket for sending the data byte[ ] buffer = message.getBytes( ); DatagramPacket datagram = new DatagramPacket(buffer, buffer.length, receiverHost, receiverPort); mySocket.send(datagram); } // end try catch (Exception ex) { System.out.println(ex); } } // end else } // end main } // end class

EXample1 Receiver

import java.net.*; import java.io.*;

/** * This example illustrates the basic method calls for connectionless * datagram socket. * @author M. L. Liu */ public class Example1Receiver {

// An application which receives a message using connectionless // datagram socket. // A command line argument is expected, in order: // // Note: the same port number should be specified in the // command-line arguments for the sender.

public static void main(String[] args) { if (args.length != 1) System.out.println ("This program requires a command line argument."); else { int port = Integer.parseInt(args[0]); final int MAX_LEN = 10; // This is the assumed maximum byte length of the // datagram to be received. try { DatagramSocket mySocket = new DatagramSocket(port); // instantiates a datagram socket for receiving the data byte[ ] buffer = new byte[MAX_LEN]; DatagramPacket datagram = new DatagramPacket(buffer, MAX_LEN); mySocket.receive(datagram); String message = new String(buffer); System.out.println(message); } // end try catch (Exception ex) { } } // end else } // end main } // end class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!