我正在创建一个 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();
}
}
您的服务器能够连接到多个客户端。在您的代码中,您已经初始化了一个
SSLServerSocket
的任务是等待其他客户 - 他们每个人都会得到它自己的 SSLSocket
.
如果您的服务器无法处理所有客户端,您可以使用类似 loadbalancer 的东西,它将传入的请求传播到不同的服务器。
连接池通常在服务器内部使用——例如用于与数据库对话。
这是否回答了您的问题?如果不是,您是打算将请求传播到多个服务器还是想了解什么是连接池?
连接池在客户端实现。
在服务器端,您所能做的就是接受连接而不终止它们,直到客户端关闭,或者直到尝试读取第二个或后续请求时发生读取超时。
关于连接池,根据你的陈述,我猜你想在客户端做 服务器端:这可以通过使用多线程来实现
下面是代码片段 服务器端
ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
while(!isStopped()){
try {
ServerSocketChannel socChnl1 = ServerSocketChannel.open();
socChnl1.socket().bind(new InetSocketAddress(port));
SocketChannel obj=socChnl1 .accept();
threadPool.execute(new WorkerThread(obj);
} catch (IOException e) {
e.printStackTrace();
}
对于客户端