e500. creating an ssl server socket.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 29 行

TXT
29
字号
An SSL server socket requires certificates that it will send to clients for authentication. The certificates must be contained in a keystore whose location must be explicitly specified (there is no default). Following the example we describe how to create and specify a keystore for the SSL server socket to use. 
    try {
        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) {
    }

Specify the keystore of certificates using the javax.net.ssl.keyStore system property: 
    > java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 MyServer


For testing purposes, you can create a keystore with a self-signed certificate, using the keytool command: 
    > keytool -keystore mySrvKeystore -keypasswd 123456 -genkey -keyalg RSA -alias mycert

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?