Skip to content

Commit ad40040

Browse files
adperezmoralespivovarit
authored andcommitted
BAEL-1531 Add class to connect to secured server (eugenp#3919)
1 parent 5a8e4a7 commit ad40040

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.ssl;
2+
3+
import java.io.InputStream;
4+
import java.io.OutputStream;
5+
6+
import javax.net.ssl.SSLSocket;
7+
import javax.net.ssl.SSLSocketFactory;
8+
9+
public class SecureConnection {
10+
11+
public static void main(String[] args) {
12+
if (args.length != 2) {
13+
System.out.println("Use: SecureConnection host port");
14+
System.exit(1);
15+
}
16+
try {
17+
String host = getHost(args);
18+
Integer port = getPort(args);
19+
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
20+
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, port);
21+
InputStream in = sslsocket.getInputStream();
22+
OutputStream out = sslsocket.getOutputStream();
23+
24+
out.write(1);
25+
26+
while (in.available() > 0) {
27+
System.out.print(in.read());
28+
}
29+
30+
System.out.println("Secured connection performed successfully");
31+
32+
} catch (Exception exception) {
33+
exception.printStackTrace();
34+
}
35+
}
36+
37+
/**
38+
* Get the host from arguments
39+
* @param args the arguments
40+
* @return the host
41+
*/
42+
private static String getHost(String[] args) {
43+
return args[0];
44+
}
45+
46+
/**
47+
* Get the port from arguments
48+
* @param args the arguments
49+
* @return the port
50+
*/
51+
private static Integer getPort(String[] args) {
52+
return Integer.parseInt(args[1]);
53+
}
54+
}

0 commit comments

Comments
 (0)