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

📄 simpleclient.java~13~

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

import java.net.*;
import java.io.*;
import javax.net.ssl.*;
import java.security.*;
import java.security.*;


public class SimpleClient {

    public static void main(String args[]) throws IOException {

        InetAddress hostIA = InetAddress.getByName(args[0]);
        String host = hostIA.getHostName();
        int port = Integer.parseInt(args[1]);

        System.out.println("USAGE: java SimpleClient host port");

        try {
            System.out.println("connecting...");
//            SSLSocketFactory sslFact =
//                    (SSLSocketFactory) SSLSocketFactory.getDefault();
//            SSLSocket c = (SSLSocket) sslFact.createSocket(host, port);


            SSLContext sslContext = null;
            try {
                sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            } catch (NoSuchProviderException ex) {
            } catch (NoSuchAlgorithmException ex) {
            }
            try {
                sslContext.init(null, null, new java.security.SecureRandom());
            } catch (KeyManagementException ex1) {
            }
            SSLSocketFactory factory = sslContext.getSocketFactory();
            SSLSocket c =	(SSLSocket)factory.createSocket("localhost", 8443);

            System.out.println("handshaking...");
            String s[] = c.getSupportedCipherSuites();
            for(int i = 0;i<s.length;i++){
                System.out.println(s[i]);
            }

            c.startHandshake();

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

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

            BufferedReader stdin =
                    new BufferedReader(
                            new InputStreamReader(System.in));

            String line;
            String strin;
            for (; ; ) {
                System.out.print("Enter a line:");
                strin = stdin.readLine();
                if (strin.length() == 0) {
                    break;
                }
                out.println(strin);
                out.flush();
                line = in.readLine();
                System.out.println("Msg from Server is: " + line);
            }

            in.close();
            out.close();
            c.close();

            System.out.println("done...");

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

⌨️ 快捷键说明

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