gpt4 book ai didi

java - 多线程服务器转换为 SSL 后,回显消息时出现问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:12:25 25 4
gpt4 key购买 nike

我有一个多线程聊天服务器,我已将其转换为使用 Java SSL 套接字。 You can see the version without SSL sockets compared to the one I converted here, on Github. (Master branch has SSL, other branch has regular sockets)

这个原始模型(没有 SSL)使用由客户端控制的“ServerThreads”通过向服务器端的“ClientThreads”发送消息来与其他客户端通信,然后将它们的消息回显给所有其他 ServerThreads。

Here is the run method of ServerThread_w_SSL (client side)

 @Override
public void run(){
System.out.println("Welcome :" + userName);

System.out.println("Local Port :" + socket.getLocalPort());
System.out.println("Server = " + socket.getRemoteSocketAddress() + ":" + socket.getPort());
//setup handshake
socket.setEnabledCipherSuites(socket.getSupportedCipherSuites());

try{
PrintWriter serverOut = new PrintWriter(socket.getOutputStream(), false);
InputStream serverInStream = socket.getInputStream();
Scanner serverIn = new Scanner(serverInStream);
// BufferedReader userBr = new BufferedReader(new InputStreamReader(userInStream));
// Scanner userIn = new Scanner(userInStream);
socket.startHandshake();

while(!socket.isClosed()){
if(serverInStream.available() > 0){
if(serverIn.hasNextLine()){
System.out.println(serverIn.nextLine());
}
}
if(hasMessages){
String nextSend = "";
synchronized(messagesToSend){
nextSend = messagesToSend.pop();
hasMessages = !messagesToSend.isEmpty();
}
serverOut.println(userName + " > " + nextSend);
serverOut.flush();
}
}

Here is the run method of ClientThread_w_SSL (server side)

@Override
public void run() {
try{
// setup
this.clientOut = new PrintWriter(socket.getOutputStream(), false);
Scanner in = new Scanner(socket.getInputStream());

//setup handshake
socket.setEnabledCipherSuites(socket.getSupportedCipherSuites());
socket.startHandshake();
// start communicating
while(!socket.isClosed()){
if(in.hasNextLine()){
String input = in.nextLine();
// NOTE: if you want to check server can read input, uncomment next line and check server file console.
System.out.println(input);
for(ClientThread_w_SSL thatClient : server.getClients()){
PrintWriter thatClientOut = thatClient.getWriter();
if(thatClientOut != null){
thatClientOut.write(input + "\r\n");
thatClientOut.flush();
}
}
}
}

原始程序使用常规套接字,但在转换为 SSL 套接字后,我遇到了一个问题:输入没有从 ClientThreads(服务器端)回显到 ServerThreads(客户端)。

在我第一次尝试转换为 SSL 时,我使用了证书、 keystore 和信任库。然后我遇到了同样的问题,就像我在这里没有它们一样,而不是只使用依赖于 JDK 附带的 cacerts 文件的默认套接字工厂。

请注意,在遇到此错误之前,首先要解决的问题是客户端和服务器之间发生的握手失败。 Because of the way SSL and the Java PrintWriter class work , 握手在第一次调用 PrintWriter.flush() 时启动,一旦客户端向服务器发送聊天消息,就会发生这种情况。这只能通过在 ClientThread(服务器)和 ServerThread(客户端)中手动启用受支持的密码套件来解决,然后至少在 ClientThread(如果不是两者)中调用 SSLSocket.StartHandshake()。

现在服务器正在接收来自客户端的消息,但不会将它们回显给客户端。

当我在调试器中运行它并尝试单步执行代码时,我发现 ClientThread 接收到客户端的消息,并通过在 PrintWriter 上为每个 ClientThread 调用 write(),然后调用 flush() 将其发回。 ServerThread 应该通过调用 InputStream.available() 来接收它以检查输入而不阻塞,但 available() 总是返回“0 字节”,因此它永远不会命中 Scanner.nextLine()

所以要么 Printwriter.write() 和 .flush() 没有发送数据,要么 InputStream.available() 没有读取数据。

编辑:经过更多的调试和测试,我只能将问题缩小到服务器端的输出。我通过让服务器在等待接收消息之前立即发送自己的消息来确定这一点,并让客户端只获取 nextLine() 而不是首先检查 available()。由于此测试失败,它表明必须以某种方式阻止仅来自服务器端的数据。

编辑 2:我更改了代码以使用 ObjectInputStreams 和 ObjectOuputStreams 而不是使用 Scanner 和 PrintWriters。现在我从我创建的用于保存字符串的可序列化类发送“消息”对象。这已解决来自服务器的消息的输出问题。如果我让客户端通过调用 readObject() 简单地等待输入,它将从服务器接收消息。但是,如果我首先使用 InputStream 的 availble() 方法,它仍然只返回 0,即使它不应该返回。由于 InputStream serverInStream 是由 socket.getInputStream() 初始化的,它会得到一个带有 ssl.InputRecord 的 ssl.AppInputStream,我猜这两者之一没有正确实现 available()。

最佳答案

我想通了:问题是available(),在Java中用SSL没用。 I got the solution from this answer.

关于java - 多线程服务器转换为 SSL 后,回显消息时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52088488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com