Question
You will develop a multithreaded client/server application. The client will send a MyString to the server. MyString class consists of a string variable and an
You will develop a multithreaded client/server application. The client will send a MyString to the server. MyString class consists of a string variable and an int variable for the length of the string. Methods for MyString include the standard Java coding methods and reverseTheString, determineLength, and getSubString. The constructor will only set the string variable. The server will set the length variable to the length of the string by calling determineLength and send the object back to the client. The Client will print the MyString object to include the String, the reverse of the String, the length of the String, and a substring of the String that includes the first character of the String through the length of the String -2. Both the client and server can run indefinitely.
Use ExecutorService and provide output statements showing the progress of the transaction on both the client and server sides.
The files are named MyString.java, Client.java, Server.java.
MyString.java
import java.io.*;
public class MyString implements Serializable
{
private String string;
private int length;
public MyString()
{
string= "";
length=0;
}
public MyString(String s, int l)
{
string= s;
length= string.length();
}
public void setString(String s)
{
string=s;
}
public void setLength(int l)
{
length=l;
}
public String getString()
{
return string;
}
public int getLength()
{
return length;
}
public String reverseTheString()
{
String temp="";
for (int i= string.length()-1; i>0;i--)
{
temp+= string.charAt(i);
}
return temp;
}
public int determineLength(String string)
{
return string.length();
}
public String getSubString(int a, int b)
{
return string.substring(a,b);
}
public String toString()
{
return ("String"+ string+ "length"+ length);
}
}
Server.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
public static void main(String args[])
{
ServerSocket serverSocket;
Socket connection;
int clientNo=0;
try
{
serverSocket = new ServerSocket(8000);
clientNo++;
ExecutorService threadExecutor= Executors.newCachedThreadPool();
while(true)
{
connection= serverSocket.accept();
System.out.println("Start thread for client"+ clientNo);
HandleClient thread= new HandleClient(connection, clientNo);
threadExecutor.execute(thread);
clientNo++;
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
class HandleClient implements Runnable
{
ServerSocket serverSocket;
Socket connection;
int clientNo;
ObjectInputStream input;
ObjectOutputStream output;
MyString string= null;
Object obj;
public HandleClient(Socket connection, int clientNo)
{
this.connection= connection;
this.clientNo= clientNo;
}
public void run()
{
try
{
input= new ObjectInputStream(connection.getInputStream());
output= new ObjectOutputStream(connection.getOutputStream());
while(true)
{
obj= input.readObject();
System.out.println(obj.toString());
if(obj instanceof MyString)
{
((userInput)obj).setUserInput(input);
}
output.writeObject(obj);
output.flush();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Client
{
public static void main (String args[])
{
Socket connection;
ObjectOutputStream output;
ObjectInputStream input;
Object obj;
MyString string= new MyString();
try
{
connection= new Socket("localhost",8000);
output= new ObjectOutputStream(connection.getOutputStream());
input = new ObjectInputStream(connection.getInputStream());
while(true)
{
System.out.println("Enter a string");
output.writeObject(string);
output.flush();
obj = (Object) input.readObject();
System.out.println(obj.toString());
}
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started