forked from hamzaouaddafe/AnnonceManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientTCP.java
67 lines (58 loc) · 2.67 KB
/
ClientTCP.java
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class ClientTCP {
public void startClient() {
System.out.println(">> start running encrypted tcp client.");
try {
/*
* // for security we are using SSL
* System.setProperty("javax.net.ssl.trustStore", "client.jsk");
* System.setProperty("javax.net.ssl.trustStorePassword", "123456");
* SSLSocketFactory socketFactory = (SSLSocketFactory)
* SSLSocketFactory.getDefault();
*
* SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", 1027);
* socket.setEnabledCipherSuites(socketFactory.getDefaultCipherSuites());
*
*/
System.setProperty("javax.net.ssl.trustStore", "client.jsk");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", 1027);
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream printStream = new PrintStream(socket.getOutputStream());
BufferedReader entryStream = new BufferedReader(new InputStreamReader(System.in));
String request = "";
String response = "";
printStream.println("connected");
while (!request.equalsIgnoreCase("bye")) {
response = bufferedReader.readLine();
System.out.println(
response.replace(ConsolePrinter.SEPARATOR, ConsolePrinter.BREAKER));
System.out.print(">> Commande ? : ");
request = entryStream.readLine();
printStream.println(request);
}
socket.close();
bufferedReader.close();
printStream.close();
entryStream.close();
System.out.println("Connection ended by client");
} catch (Exception e) {
System.err.println("Error in Client() : " + e.getMessage());
}
}
public static void main(String args[]) {
if (args.length == 0) {
new ClientTCP().startClient();
} else if (args.length == 1) {
if ("no-encryption".equals(args[0])) {
new ClientTCPNotSecure().startClient();
}
} else System.out.println("## bad arguments : java ClientTCP <option 'no-encryption'>");
}
}