嘿,我正在构建一个聊天应用程序,我以前使用常规套接字进行通信,但是想要添加加密,所以我已切换到SSL套接字。但是,自从我发送消息后立即切换到SSL套接字时,我收到了SSLHandshakeException,任何人都可以给我任何关于为什么会发生这种情况的线索吗? 下面是ServerConnection类:
import javax.net.ssl.SSLServerSocket
import javax.net.ssl.SSLServerSocketFactory
import java.net.Socket
import java.net.ServerSocket
import java.io._
import scala.io._
class ServerConnection(port: Int) {
def connect(): Socket = {
val factory = SSLServerSocketFactory.getDefault()
val serverSocket = factory.createServerSocket(port)
serverSocket.accept()
}
def getMsg(client: Socket): String = new BufferedSource(client.getInputStream()).getLines().next()
def sendMsg(client: Socket, msg: String): Unit = {
val out = new PrintStream(client.getOutputStream())
out.println(msg)
out.flush()
}
def close(client: Socket): Unit = client.close()
以下是ClientConnection类:
import javax.net.ssl.SSLSocket
import javax.net.ssl.SSLSocketFactory
import javax.net.SocketFactory
import java.net.Socket
import java.net.InetAddress
import java.net.InetSocketAddress
import java.io._
import scala.io._
import java.util.Calendar
class ClientConnection(host: InetAddress, port: Int) {
def connect(): Socket = {
val sslfactory = SSLSocketFactory.getDefault()
val sslsocket = sslfactory.createSocket(host, port)
sslsocket
}
def getMsg(server: Socket): String = new BufferedSource(server.getInputStream()).getLines().next()
def sendMsg(server: Socket, user: User, msg: String): Unit = {
val out = new PrintStream(server.getOutputStream())
out.println(this.getCurrDate() + " " + user.getUsername() + ": " + msg)
out.flush()
}
def getCurrDate(): String = "(" + Calendar.getInstance().getTime().toString().substring(11, 19) + ")"
def close(server: Socket): Unit = server.close()
}
编辑:下面是完整的堆栈跟踪
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHands
hakeException: Received fatal alert: handshake_failure
at sun.security.ssl.SSLSocketImpl.checkEOF(Unknown Source)
at sun.security.ssl.AppInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at scala.io.BufferedSource$BufferedLineIterator.next(BufferedSource.scal
a:78)
at scala.io.BufferedSource$BufferedLineIterator.next(BufferedSource.scal
a:66)
at ClientConnection.getMsg(ClientConnection.scala:20)
at Client$.main(Client.scala:20)
at Client.main(Client.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at scala.reflect.internal.util.ScalaClassLoader$$anonfun$run$1.apply(Sca
laClassLoader.scala:70)
at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaCla
ssLoader.scala:31)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext
(ScalaClassLoader.scala:101)
at scala.reflect.internal.util.ScalaClassLoader$class.run(ScalaClassLoad
er.scala:70)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(Scala
ClassLoader.scala:101)
at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:22)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39)
at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:29)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala
:65)
at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_
failure
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source
)
at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at sun.security.ssl.AppOutputStream.write(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source)
at java.io.OutputStreamWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at ClientConnection.sendMsg(ClientConnection.scala:24)
at Client$.main(Client.scala:19)
... 19 more