simpleclient.java~11~

来自「SSL的Client,还有ssl连接的服务器端」· JAVA~11~ 代码 · 共 78 行

JAVA~11~
78
字号
package com.ssl;

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


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 = SSLContext.getInstance("SSL","SunJSSE");
            sslContext.init(null, null, new java.security.SecureRandom());
            SSLSocketFactory factory = sslContext.getSocketFactory();
            SSLSocket socket =	(SSLSocket)factory.createSocket("localhost", 7002);

            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 + =
减小字号Ctrl + -
显示快捷键?