Question: Task One: 1 . Create a new Java Project called MultiThreadServer 2 . In the new project, create a package called server . 3 .
Task One:
Create a new Java Project called MultiThreadServer
In the new project, create a package called server
In the server package, create a new class called MultiThreadServer
In the MultiThreadServer class create three attributes:
ServerSocket serverSocket
Socket connectionSocket; and
int clientCount to keep track of how many clients has connected and
by extension the number of threads created
In a parameterless constructor in the MultiThreadServer class, instantiate the
relevant attributes. The Server should be listening on Port
Place a print statement which Declares that the server has started, along with the
Date and time the server started.
In the constructor, Create an infinite loop.
Inside the infinite loop, listen for and accept any client request to connect.
Place a print statement in the loop which Declares that the server is starting a
thread for a client, along with the client count and Date and time the thread
started.
Task Two:
Inside the MultiThreadServer class, create an inner class called ClientHandler
which implements the Runnable interface.
In the ClientHandler class, implement all abstract methods.
In the ClientHandler class, create three attributes: one of type Socket; another
of type DataOutp.utStream; and the third of type DataInputStream.
Create a constructor that accepts a Socket object as its argument. Inside the
constructor of the inner class, instantiate the attributes appropriately.
Handle any exceptions that may occur.
Inside the run method, create a loop that will be used to continuously serve a
client.
Inside the loop create two variables of type double: one to store a radius value
and the other to store the area of a circle.
Using the DataInputStream Objects readDouble method, accept a radius from a
client and assign the value to the variable you created above to store the radius.
Using the radius calculate the area of a circle and assign the value to the variable
you created above to store a circles area.
Using the DataOutpuStream objects writeDouble method, send the area back to
the client.
Display in the ClientHandler the radius received from the client and the
calculated area.
Handle all exceptions appropriately.
Task Three:
Inside the constructor inside the MultiThreadServer class, create an instance of
the ClientHandler class, passing it the Socket object as its argument.
Create a new Thread Object, passing the ClientHandler object cast to a Runnable,
ieRunnable obj, as its argument.
Start the thread by calling the start method on the thread object, ie
threadObj.start;
Increment the client count, ie the thread count variable.
Task Four:
Start the server and ensure all is well.
Task Five:
Using your knowledge from prior lessons, create a new Project Client application
that will connect to this MultiThreadServer When successfully connected, the
client is to:
send a double value representing a circles radius.
receive a double value representing the area of a circle, whose radius it had
sent to the server.
Write code to display the radius sent to the server and the area of the circle,
returned from the server.
Task Six:
Start the client application and test the program.
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
