⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleserver.java~5~

📁 SSL的Client,还有ssl连接的服务器端
💻 JAVA~5~
字号:
package com.ssl;

import java.io.*;
import java.net.*;
import java.security.KeyStore;
import javax.net.*;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;

public class SimpleServer {

    public static void main(String args[]) {

        int port = Integer.parseInt(args[0]);
        String passphrase = args[1];

        ServerSocket s;
        Socket c;
        System.out.println("USAGE: java SimpleServer port passphrase");

        try {
            System.out.println("creating socket...");
            ServerSocketFactory ssf =
                    SimpleServer.getServerSocketFactory(passphrase);
            s = ssf.createServerSocket(port);
            System.out.println("waiting for connection...");
            c = s.accept();

            PrintWriter out = new PrintWriter(
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    c.getOutputStream())));

            BufferedReader in =
                    new BufferedReader(
                            new InputStreamReader(c.getInputStream()));

            // echo messages from the client
            System.out.println("start echo ...");

            String line;
//				out.println("hello\nhow do you do.");

            while ((line = in.readLine()) != null) {
                System.out.println("Msg from client is: " + line);
                out.println(line);
                out.flush();
            }

            System.out.println("done...");
        } catch (IOException e) {
            System.out.println("SimpleServer died: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static ServerSocketFactory getServerSocketFactory(String passwd) {
        SSLServerSocketFactory ssf = null;
        try {
            // set up key manager to do server authentication
            SSLContext ctx;
            KeyManagerFactory kmf;
            KeyStore ks;
            char[] passphrase = passwd.toCharArray();

            ctx = SSLContext.getInstance("TLS");
            kmf = KeyManagerFactory.getInstance("SunX509");
            ks = KeyStore.getInstance("JKS");

//		ks.load(new FileInputStream("wahabPrivateStore"), passphrase);
            ks.load(new FileInputStream("askey.keystore"), passphrase);
            kmf.init(ks, passphrase);
            ctx.init(kmf.getKeyManagers(), null, null);

            ssf = ctx.getServerSocketFactory();
            return ssf;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}

⌨️ 快捷键说明

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