[Solved]-Question 1 Java Code Client Server Application Provided Appendices D Code Made Available A Q37200580
Please answer question carefully &correctly for a thumbs up.









Question 1 Java code for a client-server application is provided in Appendices A-D This code was made available in advance of the examination (a) Describe the network architecture of the server application in terms of (i) The application class and underlying Java networking classes; (ii) The purpose of that class as part of the network architecture (b) State the sequence of operations that are required to pass a string from the client to the server and back to the client State the class and method associated with each step (c) Using Appendix C: TCPProtocol.java as an example, describe the use of an interface in Java OOP design State the specific purpose of the interface defined in Appendix C. (d) Describe a class structure for a thread-per-client java.io implementation of the net- working in this application. You should describe both the client-side and server-side application Your answer should state the name and purpose of each class you would use. For each class state the most important method defined in the class relating to the networking task Java code is not required as part of the solutiorn (e) Describe the typical network activity for which the client-server implementation in Ap- pendices A-D would be preferred to a thread-per-client java.io implementation State one alternative model that could be used to achieve the same networking func- tionality. Give one reason for your answer Appendix A: TCPEchoClient.java 1 import java.net. InetSocketAddress; 2 import java.net.SocketException; 3 import java . n10 . ByteBuffer; 4 import java.nio.channels.SocketChannel; s import java.io. IOException; 8client class o public class TCPEchoClient 12 private String server – null; 1 private int servPort1; private byte argument null; private SocketChannel clntChan -null; is 16 *constructor 19 20 public TCPEchoClient( String theServer, String theData, int port throws IOException 21 server-theServer; // Server name or IP address I1 Convert input String to bytes using the default charset argumenttheData.getBytes 23 25 26 27 servPort-port; // Create channel and set to nonblocking clntChan-SocketChannel.open clntChan.configureBlocking(false) 34 *connect and communicate 7 public void connect ) throws IOException // Initiate connection to server and repeatedly poll until complete if (clntChan.connect (new InetSocketAddress (server, servPort))) while (!clntChan.finishConnct O) System.out.print(“.”); // Do something else 43 ByteBuffer writeBufByteBuffer.wrap (argument); ByteBuffer readBufByteBuffer.allocate(argument.length) int totalBytesRcvd0; I/ Total bytes received so far int bytesRcvd; // Bytes received in last read while (totalBytesRcvdargument.length) f if (writeBuf.hasRemainingO) f 50 clntChan.write(writeBuf); if ((bytesRcvd clntChan.read(readBuf)) throw new SocketException (“Connection closed prematurely”) totalBytesRcvd bytesRcvd; System.out.print(“.”); // Do something else System.out.println(“Received: ” II convert to String per default charset new String(readBuf.array O, 0, totalBytesRcvd)) clntChan.close; 64 *main o public static void main (String argsO) if ( args.length !. 3 ) // Test for correct # of args 70 71 72 73 throw new IllegalArgumentException (“Parameter (s): <Server> <Port> <Word>”); try TCPEchoclient client new TCPEchoClient args [o], args [2], Integer.parseInt (args[1]); 75 client.connect (O 76 catch IOException ioex) System.out.println(” IOException encounteredn”); 70 Appendix B: TCPServerSelector.java 1 import java.net. InetSocketAddress; 2 import java.nio.channels.SelectionKey; a import java.nio.channels.Selector; 4 import java.nio.channels.ServerSocketChannel; s import java.util.Iterator; 6 import java.io.IOException; 9 Server class 10 u public class TCPServerSelector f 12 private static final int BUFSIZE-256; I Buffer size (bytes) private static final int TIMEOUT3000; // Wait timeout (milliseconds) 15 16 private Selector selector – null; 17 private TCPProtocol protocol-null; *constructor 21 22 public TCPServerSelector( String] ports throws IOException f 23 24 25 26 27 // Create a selector to multiplex listening sockets and connections selector-Selector.open ; // Create listening socket channel for each port and register selector for (String port: ports) [ ServerSocketChannel listnChannel-ServerSocketChannel.open listnChannel.socket .bind(new InetSocketAddress (Integer.parseInt (port))); listnChannel.configureBlocking(false); I/ must be nonblocking to register // Register selector with channel. The returned key is ignored listnChannel.register (selector, SelectionKey.OP_ACCEPT) 35 // Create a handler that will implement the protocol protocol-new EchoSelectorProtocol (BUFSIZE); 43 46/ run the server 48 public void runServer throws IOException f while (true) t I/ Run forever, processing available I/0 operations 50 // Wait for some channel to be ready (or timeout) if (selector. select (TIMEOUT)-0) { // returns # of ready chans 52 System.out.print(“.”); continue; // Get iterator on set of keys with I/0 to process Iterator<SelectionKey> keyIterselector.selectedKeys O.iterator; while (keyIter.hasNext)) SelectionKey keykeyIter.nextO; // Key is bit mask // Server socket channel has pending connection requests? if (key.isAcceptable()) C 64 protocol.handleAccept (key); I1 Client socket channel has pending data? if (key.isReadableO) 70 protocol.handleRead (key); 71 73 // Client socket channel is available for vriting and // key is valid (i.e., channel not closed)? if (key.isValidO&& key.isWritable)) f 75 76 protocol.handlewrite(key); 79 // remove from set of selected keys keyIter.remove; // remove from set of selected keys 92 93main 94 s public static void main(String args) [ if (args.length < 1) { // Test for correct # of args 96 97 98 throw new IllegalArgumentException (“Parameter (s): <Port> …”); try f TCPServerSelector tss neu TCPServerSelector( args ); tss.runServer 100 101 102 10 catch IOException ioex) System.out.println(” I0 exception encounteredn”) 104 105 106 107 Appendix C: TCPProtocol.java import java.nio.channels.SelectionKey; 2 import java.io.IOException; 4 interface template for the protocol class public interface TCPProtocol s void handleAccept (SelectionKey key) throws IOException; 9 void handleRead (SelectionKey key) throws IOException; 10 void handleWrite(SelectionKey key) throws IOException; Appendix D: EchoSelectorProtocol.java 1 import java.nio.channels.SelectionKey 2 import java . n10 . channels . SocketChannel; a import java.nio.channels.ServerSocketChannel; 4 import java.nio.ByteBuffer; s import java.io.IOException; 8 implemented echo protocol o public class EchoSelectorProtocol implements TCPProtocol f private int bufSize; // Size of I/0 buffer 12 13 14 15 16 * constructor public EchoSelectorProtocol(int bufSize) [ this.bufSizebufSize; 22* accept a connection 23 public void handleAccept (SelectionKey key) throws IOExceptionf 24 SocketChannel clntChan- ((ServerSocketChannel) key.channelO).accept clntChan.configureBlocking(false); // Must be nonblocking to register // Register the selector with new channel for read and attach byte buffer clntChan.register (key.selector, SelectionKey.OP_READ, ByteBuffer.allocate (bufSize)); 25 26 27 *read data 4public void handleRead (SelectionKey key) throws I0Exception // Client socket channel has pending data SocketChannel clntChan- (SocketChannel) key.channel; ByteBuffer buf (ByteBuffer) key.attachment long bytesRead clntChan.read (buf); if (bytesRead -1) t // Did the other end close? clntChan.close else if (bytesRead > 0) // Indicate via key that reading/writing are both of interest now key. interestOps(SelectionKey .OP-READ I Selectionkey . OP-WRITE); 43 45 A4 48 write data s public void handleWrite(SelectionKey key) throus IOException 51 52 NS N2 * Channel is available for writing, and key is valid (i.e., client channel * not closed) ss // Retrieve data read earlier s ByteBuffer buf (ByteBuffer) key.attachment ); 56 s buf.flip); /1 Prepare buffer for vriting SocketChannel clntChan -(SocketChannel) key.channelO; clntChan.write (buf) 58 59 6 if (Ibuf.hasRemainingO) I/ Buffer completely vritten? // Nothing left, so no longer interested in writes key.interestops (SelectionKey.OP READ) 62 63 64 65 buf.compact O; // Make room for more data to be read in Show transcribed image text Question 1 Java code for a client-server application is provided in Appendices A-D This code was made available in advance of the examination (a) Describe the network architecture of the server application in terms of (i) The application class and underlying Java networking classes; (ii) The purpose of that class as part of the network architecture (b) State the sequence of operations that are required to pass a string from the client to the server and back to the client State the class and method associated with each step (c) Using Appendix C: TCPProtocol.java as an example, describe the use of an interface in Java OOP design State the specific purpose of the interface defined in Appendix C. (d) Describe a class structure for a thread-per-client java.io implementation of the net- working in this application. You should describe both the client-side and server-side application Your answer should state the name and purpose of each class you would use. For each class state the most important method defined in the class relating to the networking task Java code is not required as part of the solutiorn (e) Describe the typical network activity for which the client-server implementation in Ap- pendices A-D would be preferred to a thread-per-client java.io implementation State one alternative model that could be used to achieve the same networking func- tionality. Give one reason for your answer
Appendix A: TCPEchoClient.java 1 import java.net. InetSocketAddress; 2 import java.net.SocketException; 3 import java . n10 . ByteBuffer; 4 import java.nio.channels.SocketChannel; s import java.io. IOException; 8client class o public class TCPEchoClient 12 private String server – null; 1 private int servPort1; private byte argument null; private SocketChannel clntChan -null; is 16 *constructor 19 20 public TCPEchoClient( String theServer, String theData, int port throws IOException 21 server-theServer; // Server name or IP address I1 Convert input String to bytes using the default charset argumenttheData.getBytes 23 25 26 27 servPort-port; // Create channel and set to nonblocking clntChan-SocketChannel.open clntChan.configureBlocking(false) 34 *connect and communicate 7 public void connect ) throws IOException // Initiate connection to server and repeatedly poll until complete if (clntChan.connect (new InetSocketAddress (server, servPort))) while (!clntChan.finishConnct O) System.out.print(“.”); // Do something else 43
ByteBuffer writeBufByteBuffer.wrap (argument); ByteBuffer readBufByteBuffer.allocate(argument.length) int totalBytesRcvd0; I/ Total bytes received so far int bytesRcvd; // Bytes received in last read while (totalBytesRcvdargument.length) f if (writeBuf.hasRemainingO) f 50 clntChan.write(writeBuf); if ((bytesRcvd clntChan.read(readBuf)) throw new SocketException (“Connection closed prematurely”) totalBytesRcvd bytesRcvd; System.out.print(“.”); // Do something else System.out.println(“Received: ” II convert to String per default charset new String(readBuf.array O, 0, totalBytesRcvd)) clntChan.close; 64 *main o public static void main (String argsO) if ( args.length !. 3 ) // Test for correct # of args 70 71 72 73 throw new IllegalArgumentException (“Parameter (s): “); try TCPEchoclient client new TCPEchoClient args [o], args [2], Integer.parseInt (args[1]); 75 client.connect (O 76 catch IOException ioex) System.out.println(” IOException encounteredn”); 70
Appendix B: TCPServerSelector.java 1 import java.net. InetSocketAddress; 2 import java.nio.channels.SelectionKey; a import java.nio.channels.Selector; 4 import java.nio.channels.ServerSocketChannel; s import java.util.Iterator; 6 import java.io.IOException; 9 Server class 10 u public class TCPServerSelector f 12 private static final int BUFSIZE-256; I Buffer size (bytes) private static final int TIMEOUT3000; // Wait timeout (milliseconds) 15 16 private Selector selector – null; 17 private TCPProtocol protocol-null; *constructor 21 22 public TCPServerSelector( String] ports throws IOException f 23 24 25 26 27 // Create a selector to multiplex listening sockets and connections selector-Selector.open ; // Create listening socket channel for each port and register selector for (String port: ports) [ ServerSocketChannel listnChannel-ServerSocketChannel.open listnChannel.socket .bind(new InetSocketAddress (Integer.parseInt (port))); listnChannel.configureBlocking(false); I/ must be nonblocking to register // Register selector with channel. The returned key is ignored listnChannel.register (selector, SelectionKey.OP_ACCEPT) 35 // Create a handler that will implement the protocol protocol-new EchoSelectorProtocol (BUFSIZE); 43
46/ run the server 48 public void runServer throws IOException f while (true) t I/ Run forever, processing available I/0 operations 50 // Wait for some channel to be ready (or timeout) if (selector. select (TIMEOUT)-0) { // returns # of ready chans 52 System.out.print(“.”); continue; // Get iterator on set of keys with I/0 to process Iterator keyIterselector.selectedKeys O.iterator; while (keyIter.hasNext)) SelectionKey keykeyIter.nextO; // Key is bit mask // Server socket channel has pending connection requests? if (key.isAcceptable()) C 64 protocol.handleAccept (key); I1 Client socket channel has pending data? if (key.isReadableO) 70 protocol.handleRead (key); 71 73 // Client socket channel is available for vriting and // key is valid (i.e., channel not closed)? if (key.isValidO&& key.isWritable)) f 75 76 protocol.handlewrite(key); 79 // remove from set of selected keys keyIter.remove; // remove from set of selected keys
92 93main 94 s public static void main(String args) [ if (args.length 0) // Indicate via key that reading/writing are both of interest now key. interestOps(SelectionKey .OP-READ I Selectionkey . OP-WRITE); 43
45 A4 48 write data s public void handleWrite(SelectionKey key) throus IOException 51 52 NS N2 * Channel is available for writing, and key is valid (i.e., client channel * not closed) ss // Retrieve data read earlier s ByteBuffer buf (ByteBuffer) key.attachment ); 56 s buf.flip); /1 Prepare buffer for vriting SocketChannel clntChan -(SocketChannel) key.channelO; clntChan.write (buf) 58 59 6 if (Ibuf.hasRemainingO) I/ Buffer completely vritten? // Nothing left, so no longer interested in writes key.interestops (SelectionKey.OP READ) 62 63 64 65 buf.compact O; // Make room for more data to be read in
Expert Answer
Answer to Please answer question carefully & correctly for a thumbs up…. . . .
OR

