-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
82 lines (71 loc) · 2.35 KB
/
Client.java
File metadata and controls
82 lines (71 loc) · 2.35 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
82
/**
* @author Daniele Bufalo
* @version 1.0
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
// TODO: Auto-generated Javadoc
/**
* The Class Client.
*/
public class Client {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String args[]) {
String host = "127.0.0.1";
int port = 8081;
new Client(host, port);
}
/**
* Instantiates a new client.
*
* @param host the host
* @param port the port
*/
public Client(String host, int port) {
try {
String serverHostname = new String("127.0.0.1");
System.out.println("Connecting to host " + serverHostname + " on port " + port + ".");
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket(serverHostname, 8081);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Unknown host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Unable to get streams from server");
System.exit(1);
}
/** {@link UnknownHost} object used to read from console */
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print("client: ");
String userInput = stdIn.readLine();
/** Exit on 'q' char sent */
if ("q".equals(userInput)) {
break;
}
out.println(userInput);
System.out.println("server: " + in.readLine());
}
/** Closing all the resources */
out.close();
in.close();
stdIn.close();
echoSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}