Review Question: Using multi-threading, create a client and a server java applic
ID: 650776 • Letter: R
Question
Review Question: Using multi-threading, create a client and a server java applications respecting the following: 1- The server is listening to the port 12321. 2- The client sends a string to the server following this template: '' - '', where the Number can be either 1 or 2 and the text can be any text. 3- If the sent by the client is 1, the server will return the in a reversed order. For example: ''Hello'' will become: ''Olleh'' 4- If the is 2, the server will return either the text '' is a palindrome.'' Or '' is NOT a palindrome.'' A palindrome is a text which remains the same when reversed.Explanation / Answer
Server Programm:
import java.io.*;
import java.net.*;
public class Server2 {
public static void main(String args[]) {
int port = 6789;
Server2 server = new Server2( port );
server.startServer();
}
// declare a server socket and a client socket for the server;
// declare the number of connections
ServerSocket echoServer = null;
Socket clientSocket = null;
int numConnections = 0;
int port;
public Server2( int port ) {
this.port = port;
}
public void stopServer() {
System.out.println( "Server cleaning up." );
System.exit(0);
}
public void startServer() {
// Try to open a server socket on the given port
// Note that we can't choose a port less than 1024 if we are not
// privileged users (root)
try {
echoServer = new ServerSocket(port);
}
catch (IOException e) {
System.out.println(e);
}
System.out.println( "Server is started and is waiting for connections." );
System.out.println( "With multi-threading, multiple connections are allowed." );
System.out.println( "Any client can send -1 to stop the server." );
// Whenever a connection is received, start a new thread to process the connection
// and wait for the next connection.
while ( true ) {
try {
clientSocket = echoServer.accept();
numConnections ++;
Server2Connection Server2Connection(clientSocket, numConnections, this);
new Thread(oneconnection).start();
}
catch (IOException e) {
System.out.println(e);
}
}
}
}
class Server2Connection implements Runnable {
BufferedReader is;
PrintStream os;
Socket clientSocket;
int id;
Server2 server;
public Server2Connection(Socket clientSocket, int id, Server2 server) {
this.clientSocket = clientSocket;
this.id = id;
this.server = server;
System.out.println( "Connection " + id + " established with: " + clientSocket );
try {
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
os = new PrintStream(clientSocket.getOutputStream());
} catch (IOException e) {
System.out.println(e);
}
}
public void run() {
String line;
boolean serverStop = false;
try {
while (true) {
line = is.readLine();
String[] a= line.split("-");
if("1".equals(a[0]))
{
String original, reverse = "";
original = a[1];
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
{
reverse = reverse + original.charAt(i);
}
os.println("reverse is "+reverse);
}
if("2".equals(a[0]))
{
String original=a[1],reverse="";
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
os.println("string is a palindrome");
else
os.println("string is not a palindrome.");
}
if ("-1".equals(a[0]) ) {
serverStop = true;
break;
}
if ("0".equals(a[0]))
os.println("stopped" );
}
System.out.println( "Connection " + id + " closed." );
is.close();
os.close();
clientSocket.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
Client Programm:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
String hostname = "localhost";
int port = 6789;
// declaration section:
// clientSocket: our client socket
// os: output stream
// is: input stream
Socket clientSocket = null;
DataOutputStream os = null;
BufferedReader is = null;
// Initialization section:
// Try to open a socket on the given port
// Try to open input and output streams
try {
clientSocket = new Socket(hostname, port);
os = new DataOutputStream(clientSocket.getOutputStream());
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + hostname);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: " + hostname);
}
// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on the given port
if (clientSocket == null || os == null || is == null) {
System.err.println( "Something is wrong. One variable is null." );
return;
}
try {
while ( true ) {
System.out.print( "Enter an integer (0 to stop connection, -1 to stop server): " );
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String keyboardInput = br.readLine();
os.writeBytes( keyboardInput + " " );
int n = Integer.parseInt( keyboardInput );
if ( n == 0 || n == -1 ) {
break;
}
String responseLine = is.readLine();
System.out.println("Server returns its square as: " + responseLine);
}
// clean up:
// close the output stream
// close the input stream
// close the socket
os.close();
is.close();
clientSocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.