有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何创建SSL连接客户机类来连接到现有服务器?

我正在尝试为应用服务器创建SSL连接,但到目前为止,我看到的示例代码还包括创建一个客户机类和一个使用localhost作为服务器主机的迷你服务器类。在我的特定情况下,我不确定是否必须创建另一个SSL服务器类,因为我使用的是应用程序服务器,而不是本地主机。我已经附上了客户端和服务器类的当前代码。就像我之前提到的,我不确定是否需要server类,因为我已经有了一个服务器,不需要创建一个新的迷你服务器。如果没有必要,我如何在不创建其他类的情况下将客户端直接连接到该应用服务器

服务器

import java.io.DataInputStream;

import java.io.DataOutputStream;

import javax.net.ssl.SSLSocket;

import javax.net.ssl.SSLServerSocket;

import javax.net.ssl.SSLServerSocketFactory;

import java.security.Security;

public class Server{

    public static void main(String args[])
    {
        //The Port number through which this server will accept client connections
        int port = 443;
        System.setProperty("javax.net.ssl.keyStore","C:\\Windows\\System32\\myKeyStore.jks");
        //specifing the password of the keystore file
        System.setProperty("javax.net.ssl.keyStorePassword","123456");
        //This optional and it is just to show the dump of the details of the handshake process
        //System.setProperty("javax.net.debug","all");
        try
        {
            //SSLServerSocketFactory establishes the ssl context and and creates SSLServerSocket
            SSLServerSocketFactory sslServerSocketfactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
            //Create SSLServerSocket using SSLServerSocketFactory established ssl context
            SSLServerSocket sslServerSocket = (SSLServerSocket)sslServerSocketfactory.createServerSocket(port);
            System.out.println("Echo Server Started & Ready to accept Client Connection");
            //Wait for the SSL client to connect to this server
            SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept();
            //Create InputStream to recive messages send by the client
            DataInputStream inputStream = new DataInputStream(sslSocket.getInputStream());
            //Create OutputStream to send message to client
            DataOutputStream outputStream = new DataOutputStream(sslSocket.getOutputStream());
            outputStream.writeUTF("Hello Client, Say Something!");
            //Keep sending the client the message you recive unless he sends the word "close"
            while(true)
            {
                String recivedMessage = inputStream.readUTF();
                System.out.println("Client Said : " + recivedMessage);
                if(recivedMessage.equals("close"))
                {
                    outputStream.writeUTF("Bye");
                    outputStream.close();
                    inputStream.close();
                    sslSocket.close();
                    sslServerSocket.close();
                    break;
                }
                else
                {
                    outputStream.writeUTF("You Said : "+recivedMessage);
                }
            }
        }
        catch(Exception ex)
        {
            System.err.println("Error Happened : "+ex.toString());
        }
    }


}

客户

import java.io.DataInputStream;

import java.io.DataOutputStream;

import javax.net.ssl.SSLSocket;

import javax.net.ssl.SSLSocketFactory;

import java.security.Security;

import java.util.Scanner;



public class Client
{
    
    public static void main(String args[])
    {
        //The Port number through which the server will accept this clients connection
        int serverPort = 443;
        //The Server Address
        String serverName = "localhost";
        
        System.setProperty("javax.net.ssl.trustStore","C:\\Windows\\System32\\myTrustStore.jts");
        //specifing the password of the trustStore file
        System.setProperty("javax.net.ssl.trustStorePassword","123456");
        //This optional and it is just to show the dump of the details of the handshake process 
        //System.setProperty("javax.net.debug","all");
        try
        {
          

            //SSLSSocketFactory establishes the ssl context and and creates SSLSocket 
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
            //Create SSLSocket using SSLServerFactory already established ssl context and connect to server
            SSLSocket sslSocket = (SSLSocket)sslsocketfactory.createSocket(serverName,serverPort);
            //Create OutputStream to send message to server
            DataOutputStream outputStream = new DataOutputStream(sslSocket.getOutputStream());
            //Create InputStream to read messages send by the server
            DataInputStream inputStream = new DataInputStream(sslSocket.getInputStream());
            //read the first message send by the server after being connected
            System.out.println(inputStream.readUTF());
            Scanner scanner = new Scanner(System.in);

            //Keep sending sending the server the message entered by the client unless the it is "close"
            while (true)
            {
                System.out.println("Write a Message : ");
                //String messageToSend = System.console().readLine();
                String messageToSend = scanner.nextLine();
                outputStream.writeUTF(messageToSend);
                System.err.println(inputStream.readUTF());
                if(messageToSend.equals("close"))
                {
                    System.out.println("Coonection closed");
                    break;

                }
            }
        }
        catch(Exception ex)
        {
            System.err.println("Error Happened : "+ex.toString());
        }
    }
}

共 (0) 个答案