Question: Modify the provided Client code that the client is prompted to either enter a String or an int. In either case, the data is transmitted
Modify the provided Client code that the client is prompted to either enter a String or an int. In either case, the data is transmitted to the Server.
Modify the provided Server code such that it uses the following trycatch to parse the received data as a String or an Integer. If a String was sent, echo back the String to the Client, if an Integer was sent, append it to a growing ArrayList and send back the average of the elements in the ArrayList.
Object message input.readObject;
try
Integer x Integer.parseIntmessage;
catchjavalang.NumberFormatException e
String s Stringmessage;
need to instantiate the ArrayList as ArrayList since ArrayLists do not store primitive types.
need to use instanceof on both ends of the connection, and should display what is transmitted over the network on both ends
CLENT CODE
import java.ioEOFException;
import java.ioIOException;
import java.ioObjectInputStream;
import java.ioObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client
private final Scanner userInput
new ScannerSystemin;
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket client;
private final String host ;
private final int portNumber ;
public void runClient
try
connectToServer;
getStreams;
processConnection;
catch EOFException e
System.out.println
Client terminated ;
catch IOException e
eprintStackTrace;
finally
closeConnection;
private void connectToServer throws IOException
System.out.printlnAttempting connection
;
client new SocketInetAddressgetByNamehost portNumber;
System.out.printlnConnected to: client.getInetAddressgetHostName;
private void getStreams throws IOException
output new ObjectOutputStreamclientgetOutputStream;
output.flush;
input new ObjectInputStreamclientgetInputStream;
System.out.println
Got IO streams
;
private void processConnection throws IOException
String message;
do
System.out.printType message: ;
message userInput.nextLine;
ifmessage
sendDatamessage;
try
message StringinputreadObject;
System.out.println
SERVER message;
catch ClassNotFoundException e
System.out.println
Unknown object received";
while message.equalsTERMINATE;
private void closeConnection
System.out.println
Closing ;
try
output.close;
input.close;
client.close;
catch IOException e
eprintStackTrace;
private void sendDataString message
try
output.writeObjectmessage;
output.flush;
catch IOException e
System.out.println
Error writing ;
SERVER CODE
import java.ioEOFException;
import java.ioIOException;
import java.ioObjectInputStream;
import java.ioObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private final int portNumber ;
private final int backlogLimit ;
public void runServer
try
server new ServerSocketportNumber backlogLimit;
while true
try
waitForConnection;
getStreams;
processConnection;
catch EOFException e
System.out.println
Server terminated ;
finally
closeConnection;
catch IOException e
eprintStackTrace;
private void waitForConnection throws IOException
System.out.printlnWaiting connection
;
connection server.accept;
System.out.printlnconnection received: connection.getInetAddressgetHostName;
private void getStreams throws IOException
output new ObjectOutputStreamconnectiongetOutputStream;
output.flush;
input new ObjectInputStreamconnectiongetInputStream;
private void processConnection throws IOException
String message ;
do
try
message String input.readObject;
System.out.println
CLIENT message;
catch ClassNotFoundException e
System.out.println
Unknown type received";
ifmessage.equalsTERMINATE
sendDataOK Client
;
while message.equalsTERMINATE;
sendDataTERMINATE;
private void closeConnection
System.out.println
Term
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
