Created
October 26, 2018 02:22
-
-
Save sergiougalde/09550317a64d5262699c2a7b0d04f17c to your computer and use it in GitHub Desktop.
Without keystore, both the server and client will fail. This video show how to generate keystore using keytool program. Then run server and client with keystore and password. Type the following command in your command window to create a keystore n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javasslserver; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.net.ssl.SSLServerSocketFactory; | |
/** | |
* @web http://java-buddy.blogspot.com/ | |
*/ | |
public class JavaSSLServer { | |
static final int port = 8000; | |
public static void main(String[] args) { | |
SSLServerSocketFactory sslServerSocketFactory = | |
(SSLServerSocketFactory)SSLServerSocketFactory.getDefault(); | |
try { | |
ServerSocket sslServerSocket = | |
sslServerSocketFactory.createServerSocket(port); | |
System.out.println("SSL ServerSocket started"); | |
System.out.println(sslServerSocket.toString()); | |
Socket socket = sslServerSocket.accept(); | |
System.out.println("ServerSocket accepted"); | |
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); | |
try (BufferedReader bufferedReader = | |
new BufferedReader( | |
new InputStreamReader(socket.getInputStream()))) { | |
String line; | |
while((line = bufferedReader.readLine()) != null){ | |
System.out.println(line); | |
out.println(line); | |
} | |
} | |
System.out.println("Closed"); | |
} catch (IOException ex) { | |
Logger.getLogger(JavaSSLServer.class.getName()) | |
.log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment