使用SSL套接字的SSLhandShakeException Java
最近有人告诉我应该使用SSL套接字而不是常规套接字。所以我改变了一切,运行了keytool,但当我实际测试它时,当我尝试发送消息时,我得到了SSLHandShakeExceptions 这是我的客户代码: 专用静态SSLSocket插座使用SSL套接字的SSLhandShakeException Java,java,sockets,ssl,Java,Sockets,Ssl,最近有人告诉我应该使用SSL套接字而不是常规套接字。所以我改变了一切,运行了keytool,但当我实际测试它时,当我尝试发送消息时,我得到了SSLHandShakeExceptions 这是我的客户代码: 专用静态SSLSocket插座 public static void Message(String args){ System.setProperty("https.protocols", "TLSv1"); System.setProperty("javax.net.ssl.
public static void Message(String args){
System.setProperty("https.protocols", "TLSv1");
System.setProperty("javax.net.ssl.trustStore","selfsigned.jks");
try
{
String host = "localhost";
int port = 3498;
InetAddress address = InetAddress.getByName(host);
socket = (SSLSocket)SSLSocketFactory.getDefault().createSocket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String number = "Hello from the other side!";
String sendMessage = number + "\n";
bw.write(args);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
//Get the return message from the server
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我的服务器代码:
private static SSLSocket socket;
public PacketHandler() {
initListen();
}
private void initListen() {
try {
SSLServerSocket serverSocket = (SSLServerSocket)SSLServerSocketFactory.getDefault().createServerSocket(Ting.port);
System.out.println("Server Started and listening to the port "
+ Ting.port);
// Server is running always. This is done using this while(true)
// loop
while (true) {
// Reading the message from the client
socket = (SSLSocket)serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String input = br.readLine();
System.out.println("Message received from client is " + input);
/*if(PacketReader.isPacket(input)){
if(PacketReader.shouldSendDataBack(input)){
}
}*/
/*
*
* if client has data waiting or client has new data
* request new data.
* else, wait 5 seconds and repeat
*
*
*/
// Sending the response back to the client.
/*OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("");*/
//TODO Implement method to send this data to client storage data.
//bw.flush();
}
//Check for exceptions and try to close the socket.
} catch (Exception e) {
} finally {
try {
socket.close();
} catch (Exception e) {
}
}
}
错误呢
javax.net.ssl.SSLHandshakeException:收到致命警报:握手失败
位于sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
位于sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
位于sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2023)
位于sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125)
您对造成这种情况的原因有何看法?您似乎没有为服务器定义密钥库。设置
https.protocols
是无效的,除非您使用的是HttpURLConnection
。您不需要“等待5秒”:readLine()
将一直阻止,直到数据到达或流结束。您似乎没有为服务器定义密钥库。设置https.protocols
是无效的,除非您使用的是HttpURLConnection
。您不需要“等待5秒钟”:readLine()
将一直阻塞,直到数据到达或流结束。