Write a client-server application using Java sockets that allows a client to write a message (as a String) to a socket. A server will read this message, count the number of letters, digits, and any other characters than letters or digits in the message, and then send these three counts back to the client. The server will listen to port 6100. The client can obtain the String message that it is to be passed to the server by using a prompt to the user (keyboard) One strategy for sending the three counts back to the client is for the server to construct an object containing

a. The message it receives from the client
b. A count of the number of letters in the message
c. A count of the number of digits in the message
d. A count of the number of other characters in the message

Such an object can be modeled using the following interface:

public interface Message
{
// set the counts for letters, digits, and other characters
public void setCounts();

// return the number of letters
public int getLetterCount();

// return the number of digits
public int getDigitCount();

// return the number of other characters
public int getOtherCount();
}

The server will handle multiple client requests at the same time using threading. It creates a thread for each client request. The thread will first read the String (sent by the client) from the socket, construct a new object that implements the Message interface and count the number of letters, digits, and other characters in the String. The thread eventually writes the contents of the Message object to the socket. The client will send a String to the server and will wait for the server to respond with a Message containing the count of the number of letters, digits, and other characters in the message. Communication over the socket connection will require obtaining the InputStream and OutputStream for the socket.

Objects that are written to or read from an OutputStream or InputStream must be serialized and therefore must implement the java.io.Serializable interface. This interface is known as a marker interface, meaning that it actually has no methods that must be implemented; basically, any objects that implement this interface can be used with either an InputStream or an OutputStream. For this assignment, you will design an object named MessageImpl that implements both java.io.Serializable and the Message interface shown above.

Serializing an object requires obtaining a java.io.ObjectOutputStream and then writing the object using the writeObject() method in the ObjectOutputStream class. Thus, the server's activity will be organized roughly as follows:

a. Establish connection with the client
b. Create a thread that handles the client request
c. The thread does the following:
1. Read the String from the socket.
2. Create a new MessageImpl object and counting the number of letters, digits, and other characters in the message.
3. Obtain the ObjectOutputStream for the socket and write the MessageImpl object to this output stream.

Reading a serialized object requires obtaining a java.io. ObjectInputStream and then reading the serialized object using the readObject() method in the java.io.ObjectInputStream class. Therefore, the client's activity will be arranged approximately as follows:

a. Write the String to the socket.
b. Obtain the ObjectInputStream from the socket and read the MessageImpl.
c. Display the counts of the letters, digits, and other characters.

Output sample: see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.