Modify the original Example 1 code so that the sender also receives a message from the receiver. You should only need one socket only in each process. Compile, run, and turn in your code; be sure to modify the comments accordingly.

This exercise guides you through experiments with connectionless datagram socket via code sample Example1. As a start, it is recommended that you run both programs on one machine, the host name of which you may refer to as “localhost”, as in the command “java Example1Sender localhost 12345 hello” to execute Example1Sender. You may optionally repeat the exercises by running the programs on separate machines, assuming that you have access to such machines.


```
public class Example1Receiver {

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

public static void main(String[] args) {
if (args.length != 3)
System.out.println
("This program requires 3 command line arguments.");
else {
try {
int myPort = Integer.parseInt(args[0]);
InetAddress senderHost =
InetAddress.getByName(args[1]);
int senderPort = Integer.parseInt(args[2]);
final int MAX_LEN = 10;
// This is the assumed maximum byte length of the
// datagram to be received.

DatagramSocket mySocket = new DatagramSocket(myPort);
// 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);

// now send
buffer = message.getBytes( );
datagram =
new DatagramPacket(buffer, buffer.length,
senderHost, senderPort);
mySocket.send(datagram);


} // end try
catch (Exception ex) {
}
} // end else
} // end main
} // end class

/**
* This example illustrates the basic method calls for connectionless
* datagram socket. It sends then receives a message using one
* socket.
* @author M. L. Liu
*/
public class Example1Sender {

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


public static void main(String[] args) {
if (args.length != 4)
System.out.println
("This program requires four command line arguments");
else {
try {
final int MAX_LEN = 10;
int myPort = Integer.parseInt(args[0]);
InetAddress receiverHost =
InetAddress.getByName(args[1]);
int receiverPort = Integer.parseInt(args[2]);
String message = args[3];

DatagramSocket mySocket = new DatagramSocket(myPort);
// instantiates a datagram socket for sending the data
byte[ ] buffer = message.getBytes( );
DatagramPacket datagram =
new DatagramPacket(buffer, buffer.length,
receiverHost, receiverPort);
mySocket.send(datagram);

// now receive
buffer = new byte[MAX_LEN];
datagram =
new DatagramPacket(buffer, MAX_LEN);
mySocket.receive(datagram);
message = new String(buffer);
System.out.println(message);

} // end try
catch (Exception ex) {
System.out.println(ex);
}
} // end else
} // end main
} // end class

```

Computer Science & Information Technology

You might also like to view...

Which statement about hashing is false?

a. Hashing facilitates high-speed storing and retrieval of data. b. Two different data items can hash to the same cell; this is called a collision. c. A load factor of 0.5 usually results in good hashing performance, but less efficient utilization of memory. d. A load factor of 1.0 usually results in good hashing performance, but less efficient utilization of memory.

Computer Science & Information Technology

A(n) ________ keeps track of multiple entries in an attachment field and cannot be worked with or viewed

Fill in the blank(s) with correct word

Computer Science & Information Technology

LinkedIn uses __________ to examine the software for errors.

A. AI B. DSS C. ERP D. SCM

Computer Science & Information Technology

In an array-based implementation of a heap, the heapInsert operation is ______.

a) O(1) b) O(n) c) O(n2) d) O(log n)

Computer Science & Information Technology