This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                                2年前关闭。
            
                    
我一直在尝试将我在Java中使用套接字制作的客户端/服务器模型转换为SSL。生成包含服务器的根文件和中间文件的PKCS12文件后,我使用System.setProperty将客户端和服务器文件中的信任关系和密钥库更改为PKCS文件。我不太确定这是否是您的本意。我还使用SSLSocketFactory创建了必要的套接字,对套接字进行了更改。

这是下面的代码。
客户:

`public class Client
{
    private SSLSocket client_socket;
    private DataOutputStream output_stream;
    private FileReader reader;
    private BufferedReader buffer_reader;
public Client(String address, int port)
{
    System.setProperty("javax.net.ssl.keyStore", "wa.p12");
    System.setProperty("javax.net.ssl.keyStorePassword", "namihana");
    System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
    System.setProperty("javax.net.ssl.trustStore", "wa.p12");
    System.setProperty("javax.net.ssl.trustStorePassword", "namihana");
    System.setProperty("javax.net.ssl.TrustStoreType", "PKCS12");
//  System.setProperty("javax.net.ssl.trustStoreType", "PKCS12");
    Security.addProvider(new Provider());

    try
    {
    SSLSocketFactory ssf =  (SSLSocketFactory) SSLSocketFactory.getDefault();
    client_socket = (SSLSocket) ssf.createSocket("127.0.0.1", 8072);
    client_socket.startHandshake();

 public class Server
{
    public Socket socket_2;
    public SSLServerSocket server_socket;
    public InputStreamReader input_stream;
    private BufferedReader buffer_reader;
    private DataOutputStream output_sender;
    private PrintWriter output_writer;
    private boolean result;
    private char[] multichoice_answers;


服务器:

public Server(int port)
        {
            System.setProperty("javax.net.ssl.keyStore", "wa.p12");
            System.setProperty("javax.net.ssl.keyStorePassword", "namihana");
            System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
            System.setProperty("javax.net.ssl.trustStore", "wa.p12");
            System.setProperty("javax.net.ssl.trustStorePassword", "namihana");
            System.setProperty("javax.net.ssl.TrustStoreType", "PKCS12");
             multichoice_answers = new char[30];
             multichoice_answers[0] = 'A';
             multichoice_answers[1] = 'A';
             multichoice_answers[2] = 'D';
             multichoice_answers[3] = 'C';
             multichoice_answers[4] = 'C';
             multichoice_answers[5] = 'C';
             multichoice_answers[6] = 'D';
             multichoice_answers[7] = 'B';
             multichoice_answers[8] = 'D';
             multichoice_answers[9] = 'B';
             multichoice_answers[10] = 'C';
             multichoice_answers[11] = 'D';
             multichoice_answers[12] = 'A';
             multichoice_answers[13] = 'D';
             multichoice_answers[14] = 'C';

            try
        {
            SSLServerSocketFactory ssf =  (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            SSLServerSocket server_socket =  (SSLServerSocket) ssf.createServerSocket(port);
            System.out.println("Server_socket made");
            Security.addProvider(new Provider());
            //server_socket.setSoTimeout(100000);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
     }


    public void receive()
    {
        try
        {
             System.out.println("Server connected2");
             socket_2 =   server_socket.accept();

最佳答案

您的ServerSocket块中有一个局部的try变量,该变量遮蔽了类成员。

关于java - ServerSocket在转换为SSLSocket时不接受(空指针异常),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50472933/

10-09 20:36