Samples JDK
EchoClientTest.java
1 package com.freemindcafe.socket.ssl.sample3;
2 
3 import static com.freemindcafe.utils.FileSystemUtils.currentDir;
4 import javax.net.ssl.SSLSocket;
5 import javax.net.ssl.SSLSocketFactory;
6 
7 import org.junit.Test;
8 
9 import java.io.*;
10 
11 /**
12  * @author IC033920
13  *
14  */
15 public class EchoClientTest {
16 
17  @Test
18  public void simple_client_connection_with_server_succeeds_as_client_and_server_keystore_has_each_others_key(){
19  try {
20 
21  System.setProperty(
22  "javax.net.ssl.trustStore",currentDir()+"/src/com/freemindcafe/socket/ssl/sample3/clientkeystore.jks");
23  System.setProperty("javax.net.ssl.trustStorePassword", "password");
24  System.setProperty(
25  "javax.net.ssl.keyStore",
26  currentDir()+"/src/com/freemindcafe/socket/ssl/sample3/clientkeystore.jks");
27  System.setProperty("javax.net.ssl.keyStorePassword", "password");
28  System.setProperty("javax.net.debug", "ssl:handshake");
29 
30 
31  SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory
32  .getDefault();
33  SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(
34  "localhost", 9999);
35 
36  InputStream inputstream = System.in;
37  InputStreamReader inputstreamreader = new InputStreamReader(
38  inputstream);
39  BufferedReader bufferedreader = new BufferedReader(
40  inputstreamreader);
41 
42  OutputStream outputstream = sslsocket.getOutputStream();
43  OutputStreamWriter outputstreamwriter = new OutputStreamWriter(
44  outputstream);
45  BufferedWriter bufferedwriter = new BufferedWriter(
46  outputstreamwriter);
47 
48  String string = null;
49  while ((string = bufferedreader.readLine()) != null) {
50  bufferedwriter.write(string + '\n');
51  bufferedwriter.flush();
52  }
53  } catch (Exception exception) {
54  exception.printStackTrace();
55  }
56  }
57 }