-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringReverseClient.java
More file actions
71 lines (62 loc) · 2.36 KB
/
Copy pathStringReverseClient.java
File metadata and controls
71 lines (62 loc) · 2.36 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
* Client
*/
public class StringReverseClient {
/**
* This method creates a client socket with servers' IP and port and connects to the servers' socket.
* @param ipAddress: Servers' IP address
* @param port : Servers' port at which it is listening
*/
public void connectToServer(String ipAddress, Integer port){
Socket client = null;
PrintWriter outputToServer;
try{
//creating client socket
client = new Socket(ipAddress, port);
//creating input readers and output writers
BufferedReader readFromServer = new BufferedReader((new InputStreamReader(client.getInputStream())));
outputToServer = new PrintWriter(client.getOutputStream(), true);
//creating input reader for the user
Scanner sc = new Scanner(System.in);
//initial response from the server
String response = readFromServer.readLine();
System.out.println(response);
String input = "";
//this will run till the user inputs 's' as the string.
while(!"s".equalsIgnoreCase(input)) {
input = sc.nextLine();
if("s".equalsIgnoreCase(input)){
break;
}
//sending request for string reversal
outputToServer.println(input);
//recieving response from the server as the reversed string
response = readFromServer.readLine();
//displaying the response to the user console
System.out.println(response);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(null != client && !client.isClosed()){
try{
client.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
public static void main(String[] args) {
StringReverseClient s = new StringReverseClient();
String host = "127.0.0.1";
int p = 6577;
s.connectToServer(host,p);
}
}