Creating an SSL Server Socket - Java Network

Java examples for Network:SSL

Description

Creating an SSL Server Socket

Demo Code

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;

public class Main {
  public static void main(String[] argv) {
    try {//from  w  w w.  jav  a2  s.c  o m
      int port = 443;
      ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
      ServerSocket ssocket = ssocketFactory.createServerSocket(port);

      // Listen for connections
      Socket socket = ssocket.accept();

      // Create streams to securely send and receive data to the client
      InputStream in = socket.getInputStream();
      OutputStream out = socket.getOutputStream();

      // Read from in and write to out...

      // Close the socket
      in.close();
      out.close();
    } catch (IOException e) {
    }
  }
}
> java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 MyServer
> keytool -keystore mySrvKeystore -keypasswd 123456 -genkey -keyalg RSA -alias mycert

Related Tutorials