In this project you are required to enhance the ECHO clients
In this project, you are required to enhance the ECHO client/server program that we discussed in the
class. You need put up a simple client/server application to accomplish the following function:
Server side program should listen on port 9999 for incoming request.
User(e.g. me) runs the client program to connect to server. Client program reads a sentence typed
in by user via keyboard and user can select a command, then it sends both the command and the
sentence to the server
Upon receipt of the sentence and command, the server converts all the letters in the sentence into
uppercase letters or lower letters or other format based on the command, and sends it back to the
client.
Client program displays the replied sentence on the screen.
(70%) You need to build this application on top of either TCP or UDP sockets. Your server should
support at least three commands: all uppercase, all lowercase, and another commands that you
designed(something like initial caps, reverse each word, reverse the entire sentense, etc.). A set of
sample Python code has been introduced in the class, please refer to the slides of Chapter 2. Other
programming languages are also allowed.
(20%) You also need to provide a document describing your protocol, i.e., underlying transport layer
service, port numbers, the order and format of message sent and received by client/server, action
associated with each message, etc. The document should provide enough information for others to
implement client and server program to interact with yours.
(10%) You should also need to discuss the following issues:
How TCP or UDP would affect your application? Why you pick TCP over UDP?(Or vice versa)
Is your protocol stateful or stateless? How does the other type of design would affect your
application?
Solution
The client server application is divided into two parts: 1> server 2> client
Below is an simple client-server application code (following your program user requirements) which works on TCP/IP protocol.
client.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class Client
{
private static Socket socket;
public static void main(String args[])
{
try
{
String host = \"localhost\";
int port = 9999;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
Scanner sc=new Scanner(System.in);
System.out.println(\"Enter your sentence :\");
String sentence=sc.next();
System.out.println(\"Enter the command\ 1-to uppercase\ 2-to lowercase\ 3-reverse\");
int command=sc.nextInt();
String sendMessage =command+sentence;
bw.write(sendMessage);
bw.flush();
System.out.println(\"Message sent to the server : \"+sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println(\"Message received from the server : \" +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
------------------------------------------------------------------------------------------------------
server.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
private static Socket socket;
public static void main(String[] args)
{
try
{
int port = 9999;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println(\"Server Started and listening to the port 9999\");
//Server is running always. This is done using this while(true) loop
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String Message = br.readLine();
System.out.println(\"Message received from client is \"+Message);
//process the Message based on command
int command=Message.charAt(0);
String sentence=Message.substring(1);
String returnMessage=\"\";
if(command==1) //toUpperCase sentence
{
returnMessage=sentence.toUpperCase();
}
else if(command==2) // toLowerCase sentence
{
returnMessage=sentence.toLowerCase();
}
else if(command==3) //reverse the sentence
{
returnMessage=new StringBuffer(sentence).reverse().toString();
}
else
returnMessage=\"wrong command\";
//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
System.out.println(\"Message sent to the client is \"+returnMessage);
bw.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}
---------------------------------------------------------------------------------------------------------
feel free to reach out if you have any doubt. keep learning :)



