In this project, you are required to enhance the ECHO client/server program that
ID: 3854781 • Letter: I
Question
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. 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 sentence, 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. You'll receive o if your programs contains syntax errors! 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.Explanation / Answer
Server
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
* A TCP server that runs on port 9999. When a client connects, it
* sends the client the current date and time, then closes the
* connection with that client. Arguably just about the simplest
* server you can write.
*/
public class Server { public static void main(String[] args) throws Exception {
System.out.println("The server is running.");
int clientNumber = 0;
ServerSocket listener = new ServerSocket(9999);
try {
while (true) {
new Square(listener.accept(), clientNumber++).start();
}
} finally {
listener.close();
}
}
/**
* A private thread to handle square requests on a particular
* socket. The client terminates the dialogue by sending a single line
* containing only a period.
*/
private static class Square extends Thread {
private Socket socket;
private int clientNumber;
public Square(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("New connection with client# " + clientNumber + " at " + socket);
}
/**
* Services this thread's client by first sending the
* client a welcome message then repeatedly reading strings
* and sending back the operationed version of the string.
*/
public void run() {
try {
// Decorate the streams so we can send characters
// and not just bytes. Ensure output is flushed
// after every newline.
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// Send a welcome message to the client.
out.println("Hello, you are client #" + clientNumber + ".");
out.println("Enter a line with only a period to quit ");
// Get messages from the client, line by line; return them
// operationed
while (true) {
String input = in.readLine();
if (input == null || input.equals(".")) {
break;
}
String sp[]=input.split("_");
if(sp[0].toUpperCase().equals("UPPERCASE"))
out.println(sp[1].toUpperCase());
else
out.println(sp[1].toLowerCase());
}
} catch (IOException e) {
log("Error handling client# " + clientNumber + ": " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
log("Couldn't close a socket, what's going on?");
}
log("Connection with client# " + clientNumber + " closed");
}
}
/**
* Logs a simple message. In this case we just write the
* message to the server applications standard output.
*/
private void log(String message) {
System.out.println(message);
}
}}
Client
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* Trivial client for the date server.
*/
public class Client {
private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame("Capitalize Client");
private JTextField dataField = new JTextField(40);
private JTextArea messageArea = new JTextArea(8, 60);
/**
* Constructs the client by laying out the GUI and registering a
* listener with the textfield so that pressing Enter in the
* listener sends the textfield contents to the server.
*/
public Client() {
// Layout GUI
messageArea.setEditable(false);
frame.getContentPane().add(dataField, "North");
frame.getContentPane().add(new JScrollPane(messageArea), "Center");
// Add Listeners
dataField.addActionListener(new ActionListener() {
/**
* Responds to pressing the enter key in the textfield
* by sending the contents of the text field to the
* server and displaying the response from the server
* in the text area. If the response is "." we exit
* the whole application, which closes all sockets,
* streams and windows.
*/
public void actionPerformed(ActionEvent e) {
out.println(dataField.getText());
String response;
try {
response = in.readLine();
if (response == null || response.equals("")) {
System.exit(0);
}
} catch (IOException ex) {
response = "Error: " + ex;
}
messageArea.append(response + " ");
dataField.selectAll();
}
});
}
/**
* Implements the connection logic by prompting the end user for the
* server's IP address, connecting, setting up streams, and consuming the
* welcome messages from the server. The Square protocol says that the
* server sends three lines of text to the client immediately after
* establishing a connection.
*/
public void connectToServer() throws IOException {
// Get the server address from a dialog box.
String serverAddress = JOptionPane.showInputDialog(frame, "Enter IP Address of the Server:",
"Welcome to the Program", JOptionPane.QUESTION_MESSAGE);
// Make connection and initialize streams
Socket socket = new Socket(serverAddress, 9999);
String operation = JOptionPane.showInputDialog(frame, "Enter Operastion(UpperCase/LowerCase):",
"Welcome to the Program", JOptionPane.QUESTION_MESSAGE);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.append(operation+"_");
// Consume the initial welcoming messages from the server
for (int i = 0; i < 3; i++) {
messageArea.append(in.readLine() + " ");
}
}
/**
* Runs the client application.
*/
public static void main(String[] args) throws Exception {
Client client = new Client();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.pack();
client.frame.setVisible(true);
client.connectToServer();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.