Question: 1.) Modify the original Example5*.java so that the ConnectionRequestor sends a replying message. to the ConnectionAcceptor after receiving a message from the Acceptor. The replying

1.) Modify the original Example5*.java so that the ConnectionRequestor sends a replying message. to the ConnectionAcceptor after receiving a message from the Acceptor. The replying message should be displayed by the Acceptor.

import java.net.*;

import java.io.*;

public class Example5ConnectionAcceptor {

// An application which receives a message using stream-mode socket

// Two command line arguments are expected, in order:

//

//

public static void main(String[] args) {

if (args.length != 2)

System.out.println

("This program requires three command line arguments");

else {

try {

int portNo = Integer.parseInt(args[0]);

String message = args[1];

// instantiates a socket for accepting connection

ServerSocket connectionSocket = new ServerSocket(portNo);

/**/ System.out.println("now ready accept a connection");

// wait to accept a connecion request, at which

// time a data socket is created

MyStreamSocket dataSocket =

new MyStreamSocket(connectionSocket.accept());

/**/ System.out.println("connection accepted");

dataSocket.sendMessage(message);

/**/ System.out.println("message sent");

dataSocket.close( );

/**/ System.out.println("data socket closed");

connectionSocket.close( );

/**/ System.out.println("connection socket closed");

} // end try

catch (Exception ex) {

ex.printStackTrace( );

} // end catch

} // end else

} // end main

} // end class

import java.net.*;

import java.io.*;

public class Example5ConnectionRequestor {

// An application that sends a message using stream-mode socket.

// Two command line arguments are expected:

//

//

//

public static void main(String[] args) {

if (args.length != 2)

System.out.println

("This program requires two command line arguments");

else {

try {

String acceptorHost = args[0];

int acceptorPort = Integer.parseInt(args[1]);

// instantiates a data socket

MyStreamSocket mySocket =

new MyStreamSocket(acceptorHost, acceptorPort);

/**/ System.out.println("Connection request granted");

String message = mySocket.receiveMessage( );

/**/ System.out.println("Message received:");

System.out.println("\t" + message);

mySocket.close( );

/**/ System.out.println("data socket closed");

} // end try

catch (Exception ex) {

ex.printStackTrace( );

}

} // 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!