我最近被告知我应该在常规套接字上使用SSL套接字 . 所以我改变了一切,运行了keytool,但是当我实际测试它时,当我尝试发送消息时,我得到了SSLHandShakeExceptions .
这是我的客户端代码:private static SSLSocket socket;
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:154)的sun.security.ssl.Alerts.getSSLException(Alerts.java:192)收到致命警报:handshake_failure at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2023)at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125)
对这是什么造成的任何想法?