-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.java
More file actions
59 lines (57 loc) · 1.44 KB
/
client.java
File metadata and controls
59 lines (57 loc) · 1.44 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class client {
public static void main(String[] args) {
// TODO Auto-generated method stub
//default host and port
String host = "localhost";
int port = 1222;
if (args.length != 2) {
System.err.println("Considering localhost and Port 1222");
// System.exit(1);
}
else {
host = args[0];
port = Integer.parseInt(args[1]);
}
try {
Socket socket = new Socket(host, port);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new
PrintWriter(socket.getOutputStream(), true);
BufferedReader stdIn = new BufferedReader(new
InputStreamReader(System.in));
String inp;
while ((inp = stdIn.readLine()) != null) {
out.println(inp);
System.out.println("Received from Server: " +
reverseString(in.readLine()));
}
in.close();
out.close();
socket.close();
} catch (UnknownHostException e) {
System.out.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
public static String reverseString(String s) {
StringBuilder rev = new StringBuilder(s.length() + 1);
String[] words = s.split(" ");
for (int i = words.length - 1; i >= 0; i--) {
rev.append(words[i]);
rev.append(" ");
}
rev.setLength(rev.length() - 1);
String rever = rev.toString();
return (rever);
}
}