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 try-catch 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.parseInt(message);
}
catch(java.lang.NumberFormatException e)
{
String s =(String)message;
}
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.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client
{
private final Scanner userInput =
new Scanner(System.in);
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket client;
private final String host ="127.0.0.1";
private final int portNumber =12345;
public void runClient()
{
try
{
connectToServer();
getStreams();
processConnection();
}
catch (EOFException e)
{
System.out.println("
Client terminated ");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
closeConnection();
}
}
private void connectToServer() throws IOException
{
System.out.println("Attempting connection
");
client = new Socket(InetAddress.getByName(host), portNumber);
System.out.println("Connected to: "+ client.getInetAddress().getHostName());
}
private void getStreams() throws IOException
{
output = new ObjectOutputStream(client.getOutputStream());
output.flush();
input = new ObjectInputStream(client.getInputStream());
System.out.println("
Got I/O streams
");
}
private void processConnection() throws IOException
{
String message;
do
{
System.out.print("Type message: ");
message = userInput.nextLine();
if(message !=""){
sendData(message);
}
try
{
message =(String)input.readObject();
System.out.println("
SERVER>>>"+ message);
}
catch (ClassNotFoundException e)
{
System.out.println("
Unknown object received");
}
} while (!message.equals("TERMINATE"));
}
private void closeConnection()
{
System.out.println("
Closing ");
try
{
output.close();
input.close();
client.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void sendData(String message)
{
try
{
output.writeObject(message);
output.flush();
}
catch (IOException e)
{
System.out.println("
Error writing ");
}
}
}
SERVER CODE
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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 =12345;
private final int backlogLimit =100;
public void runServer()
{
try
{
server = new ServerSocket(portNumber, backlogLimit);
while (true)
{
try
{
waitForConnection();
getStreams();
processConnection();
}
catch (EOFException e)
{
System.out.println("
Server terminated ");
}
finally
{
closeConnection();
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void waitForConnection() throws IOException
{
System.out.println("Waiting connection
");
connection = server.accept();
System.out.println("connection received: "+ connection.getInetAddress().getHostName());
}
private void getStreams() throws IOException
{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
}
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");
}
if(!message.equals("TERMINATE"))
{
sendData("OK, Client
");
}
} while (!message.equals("TERMINATE"));
sendData("TERMINATE");
}
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 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 Programming Questions!