-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerThread.java
More file actions
81 lines (72 loc) · 2.69 KB
/
ServerThread.java
File metadata and controls
81 lines (72 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.*;
import java.net.Socket;
/**
* Thread Class
*/
public class ServerThread implements Runnable{
Socket socket;
Integer client;
public Integer getClient() {
return client;
}
public void setClient(Integer client) {
this.client = client;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
/**
* Reverses the string
* @param input : A string that needs to reveresed
* @return reversed string
*/
public String reverseString(String input){
StringBuilder sb = new StringBuilder(input);
return sb.reverse().toString();
}
/**
* this method is called whenever start() method is called over this class' object.
*/
@Override
public void run() {
PrintWriter outputToClient = null;
try {
//creating input reader and output writer.
outputToClient = new PrintWriter(socket.getOutputStream(), true);
BufferedReader readFromClient = new BufferedReader((new InputStreamReader(this.socket.getInputStream())));
outputToClient.println("Welcome Client "+ this.getClient()+", Please provide string to be reversed " );
//receiving request from the client
String request = "";
while(!"s".equalsIgnoreCase(request)) {
request = readFromClient.readLine();
System.out.println("Received request from Client " + this.getClient() + " to reverse the string: '"+request+"'");
//calling reverse method
String response;
if(null != request)
response = this.reverseString(request);
else
response = "Null String recived";
//sending the response to client
outputToClient.println("Reverse is: '"+ response + "' #press 's' to stop or input another string to continue..");
System.out.println("Processing Complete for Client " + this.getClient());
}
}catch(IOException ioe){
System.out.println("ERROR : "+ ioe.getMessage() + " for Client "+getClient());
}finally {
if(null != this.socket && !this.socket.isClosed()){
try{
this.socket.close();
}
catch(Exception e){
System.out.println(e.getMessage() + getClient());
}
}
if(null != outputToClient){
outputToClient.close();
}
}
}
}