JAVA中的SSL套接字连接池

时间:2015-08-19 10:19:15

标签: java multithreading sockets ssl connection-pooling

我正在创建一个SSL服务器客户端。到目前为止,我所实现的是一个简单的服务器,可以与单个客户端通信(没有线程,即)。现在,我想为多个客户端扩展此应用程序,我想为此使用连接池。现在,我是否需要使用线程,或者是否有我可以使用的内置库。任何例子,链接等都可以。

PS:我尝试使用谷歌搜索,但没有找到合适的链接。

如果需要,以下是我的代码:

服务器:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.*;

public class SSLServer {

    public static void main(String args[]){
        String ksname = "file.jks";
        char kspass[] = "pass".toCharArray();
        char ctpass[] = "pass".toCharArray();

        try {
            KeyStore ks = KeyStore.getInstance("JKS");
            ks.load(new FileInputStream(ksname), kspass);
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            kmf.init(ks, ctpass);
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(kmf.getKeyManagers(), null, null);
            SSLServerSocketFactory ssf = sc.getServerSocketFactory();
            SSLServerSocket s = (SSLServerSocket) ssf.createServerSocket(4321);
            //printServerSocketInfo(s);
            SSLSocket c = (SSLSocket) s.accept();
            //printSocketInfo(c);

            BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream()));
            BufferedWriter w = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));

            w.write("Server starts\n");

            w.flush();
            String k = null;
            while((k = r.readLine()) != null){
                //do something
                if(k.equals("end"))
                    break;
                w.write(resolve(k));
                w.newLine();
                w.flush();
            }
            w.close();
            r.close();
            c.close();
            s.close();

        } catch (KeyStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (CertificateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (KeyManagementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

    private static void printSocketInfo(SSLSocket s) {
          System.out.println("Socket class: "+s.getClass());
          System.out.println("   Remote address = "
             +s.getInetAddress().toString());
          System.out.println("   Remote port = "+s.getPort());
          System.out.println("   Local socket address = "
             +s.getLocalSocketAddress().toString());
          System.out.println("   Local address = "
             +s.getLocalAddress().toString());
          System.out.println("   Local port = "+s.getLocalPort());
          System.out.println("   Need client authentication = "
             +s.getNeedClientAuth());
          SSLSession ss = s.getSession();
          System.out.println("   Cipher suite = "+ss.getCipherSuite());
          System.out.println("   Protocol = "+ss.getProtocol());
    }

    private static void printServerSocketInfo(SSLServerSocket s) {
          System.out.println("Server socket class: "+s.getClass());
          System.out.println("   Socket address = "
             +s.getInetAddress().toString());
          System.out.println("   Socket port = "
             +s.getLocalPort());
          System.out.println("   Need client authentication = "
             +s.getNeedClientAuth());
          System.out.println("   Want client authentication = "
             +s.getWantClientAuth());
          System.out.println("   Use client mode = "
             +s.getUseClientMode());
    }

    private static String resolve(String p){
        //some implementation
        return "something";
    }

    }

客户端:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.UnknownHostException;

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

public class SSLClient {

    public static void main(String[] args){

        SSLSocketFactory f = (SSLSocketFactory) SSLSocketFactory.getDefault();

        try {
            SSLSocket c = (SSLSocket) f.createSocket("localhost", 4321);
            printSocketInfo(c);
            c.startHandshake();
            BufferedWriter w = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
            BufferedReader r = new BufferedReader(new InputStreamReader(c.getInputStream()));

            //to input hex code message
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String k = null;
            while((k = r.readLine()) != null){
                //send message to server
                System.out.println(k);
                System.out.flush();
                k = in.readLine();
                if(k.equals("."))
                    break;
                System.out.println(k);
                System.out.flush();
                w.write(k);
                w.newLine();
                w.flush();
            }
            w.close();
            r.close();
            c.close();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:1)

您的服务器可以连接到多个客户端。在您的代码中,您已经初始化了SSLServerSocket等待其他客户的任务 - 每个客户都会获得自己的SSLSocket

如果您的服务器无法处理所有客户端,您可以使用类似 loadbalancer 的内容,将传入的请求分散到不同的服务器。

连接池通常在服务器内使用 - 例如与数据库交谈。

这是否回答了你的问题?如果没有,您是打算将请求分散到多个服务器还是要了解连接池的用途?

答案 1 :(得分:0)

连接池在客户端实现。

在服务器端,您所能做的就是接受连接,直到客户端关闭才终止它们,或直到尝试读取第二个或后续请求时发生某些读取超时。