SSL客户端:java.net.SocketException:缺省SSL上下文init失败:null

时间:2012-03-16 18:56:13

标签: java ssl client-server

当我尝试通过SSL连接将客户端连接到我的服务器时,我收到错误“java.net.SocketException:Default SSL context init failed:null”。我不明白这个错误。一旦我尝试将命令发送到我的服务器

,就会发生这种情况
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.StringTokenizer;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class SSL_Client {


public static void main(String[] args) throws IOException{
//  TCP_Client newClient = new TCP_Client();
//  Lock lock = new ReentrantLock();
    boolean validInput = false;
    int port = 2018;

    System.setProperty("javax.net.ssl.trustStore", "C:/Program Files/Java/jre6/lib/security/cacerts");  
    System.setProperty("javax.net.ssl.keyStore","mycert");
    System.setProperty("javax.net.ssl.keyStorePassword","123456");
    String trustStore = System.getProperty("javax.net.ssl.trustStore");
    trustStore = System.getProperty("javax.net.ssl.trustStore");

    if (trustStore == null) {
        System.out.println("javax.net.ssl.trustStore is not defined");
    } else {
        System.out.println("javax.net.ssl.trustStore = " + trustStore);
    }
    if (trustStore == null) {
        String storeLoc;
        storeLoc = System.getProperty("java.class.path");
        System.out.println("classpath: " + storeLoc);
    }


    BufferedReader din;
    PrintStream pout;
    BufferedReader stdinp = new BufferedReader(new InputStreamReader(System.in));

    String line = "done";
    StringTokenizer st;
    String hostname; 
    String task = "done";

    if(args.length>0)
        hostname = args[0];
    else
        hostname = "localhost";


    while(true)
    {
        try{
            //read input
            while(!validInput)
            {
                System.out.println("Please enter a valid command or 'done' to finish.");
                line = stdinp.readLine();
                st = new StringTokenizer(line);
                task = st.nextToken();
                if(task.equals("reserve") || task.equals("search") || task.equals("delete") || task.equals("getinfo") || task.equals("done"))
                {
                    validInput =true;
                    break;
                }
                System.out.println("Invalid command.  Please enter another command or 'done' to escape.");
            }
            if(task.equals("done"))
            {
                break;
            }
            validInput = false;//reset for next line read in

            //create a new socket every time
            SocketFactory socketFactory = SSLSocketFactory.getDefault();
            //Socket socket = socketFactory.createSocket(hostname, port);
            //SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
            SSLSocket socket = (SSLSocket)socketFactory.createSocket(hostname, port);
            System.out.println("Creating a SSL Socket For " + hostname + " on port " + port);

            // Connect to the server
            ((SSLSocket) socket).startHandshake();
            System.out.println("Handshaking Complete");

            din = new BufferedReader (new InputStreamReader (socket.getInputStream()));
            pout = new PrintStream (socket.getOutputStream());

            pout.println(line);
            pout.flush();

            //print out response from server
            System.out.println(din.readLine());

            // Close the socket
            socket.close();
            din.close();
            pout.close();

        } catch (Exception e){
            System.err.println("Server aborted: " + e);
        }
    }   
}
}

0 个答案:

没有答案