diff --git a/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLScocketClient.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLScocketClient.java new file mode 100644 index 0000000000..286ecfc9df --- /dev/null +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLScocketClient.java @@ -0,0 +1,41 @@ +package com.baeldung.httpsclientauthentication; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.net.SocketFactory; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; + +public class SSLScocketClient { + + 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" }); + InputStream is = new BufferedInputStream(socket.getInputStream()); + String message = "Hello World Message"; + System.out.println("sending message: " + message); + OutputStream os = new BufferedOutputStream(socket.getOutputStream()); + os.write(message.getBytes()); + os.flush(); + byte[] data = new byte[2048]; + int len = is.read(data); + if (len <= 0) { + throw new IOException("no data received"); + } + System.out.printf("client received %d bytes: %s%n", len, new String(data, 0, len)); + } + } + + public static void main(String[] args) throws IOException { + + startClient("localhost", 8443); + } +} diff --git a/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLSocketEchoServer.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLSocketEchoServer.java new file mode 100644 index 0000000000..66efa18fe9 --- /dev/null +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLSocketEchoServer.java @@ -0,0 +1,46 @@ +package com.baeldung.httpsclientauthentication; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +import javax.net.ServerSocketFactory; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; + +public class SSLSocketEchoServer { + + static void startServer(int port) throws IOException { + + ServerSocketFactory factory = SSLServerSocketFactory.getDefault(); + + try (SSLServerSocket listener = (SSLServerSocket) factory.createServerSocket(port)) { + listener.setNeedClientAuth(true); + listener.setEnabledCipherSuites(new String[] { "TLS_AES_128_GCM_SHA256" }); + listener.setEnabledProtocols(new String[] { "TLSv1.3" }); + System.out.println("listening for messages..."); + try (Socket socket = listener.accept()) { + InputStream is = new BufferedInputStream(socket.getInputStream()); + OutputStream os = new BufferedOutputStream(socket.getOutputStream()); + byte[] data = new byte[2048]; + int len = is.read(data); + if (len <= 0) { + throw new IOException("no data received"); + } + String message = new String(data, 0, len); + System.out.printf("server received %d bytes: %s%n", len, message); + String response = message + " processed by server"; + os.write(response.getBytes(), 0, response.getBytes().length); + os.flush(); + } + System.out.println("message processed, exiting"); + } + } + + public static void main(String[] args) throws IOException { + startServer(8443); + } +} \ No newline at end of file