Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Horse Betting The Road To Absolute Horse Racing 2

Authors: NAKAGAWA,YUKIO

1st Edition

B0CFZN219G, 979-8856410593

More Books

Students also viewed these Databases questions

Question

Identify the major criticisms of neurofinance research.

Answered: 1 week ago

Question

What is the answer to the following formula: 2 + 4* (3-4)/2

Answered: 1 week ago