-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
80 lines (68 loc) · 3 KB
/
Copy pathClient.java
File metadata and controls
80 lines (68 loc) · 3 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
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class Client {
public static void startClient(String host, int port) throws IOException {
SocketFactory factory = SSLSocketFactory.getDefault();
try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
socket.setEnabledCipherSuites(new String[] { "TLS_AES_128_GCM_SHA256" });
socket.setEnabledProtocols(new String[] { "TLSv1.3" });
String message = "This is a secure channel!";
System.out.println("sending message: " + message);
OutputStream os = new BufferedOutputStream(socket.getOutputStream());
os.write(message.getBytes());
os.flush();
InputStream is = new BufferedInputStream(socket.getInputStream());
byte[] data = new byte[2048];
int len = is.read(data);
System.out.printf("client received %d bytes: %s%n", len, new String(data, 0, len));
String line = "";
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Type: 'Exit' to close the connection");
while (!line.equals("Exit")) {
try {
line = scanner.nextLine();
;
System.out.println("sending message: " + line);
os = new BufferedOutputStream(socket.getOutputStream());
os.write(line.getBytes());
os.flush();
is = new BufferedInputStream(socket.getInputStream());
data = new byte[2048];
len = is.read(data);
System.out.printf("client received %d bytes: %s%n", len, new String(data, 0, len));
} catch (IOException i) {
System.out.println(i);
return;
}
}
try {
scanner.close();
is.close();
os.close();
socket.close();
} catch (IOException i) {
System.out.println(i);
return;
}
}
}
}
public static void main(String args[]) throws IOException {
System.setProperty("javax.net.ssl.keyStore", "user.p12");
System.setProperty("javax.net.ssl.keyStorePassword", "changeme");
System.setProperty("javax.net.ssl.trustStore", "usertruststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "changeme");
if (args.length == 1) {
int port = Integer.parseInt(args[0]);
startClient("localhost", port);
} else
return;
}
}