我最近在这里问过很多问题,事实证明这完全归功于一件事,我从网站上下载的SSL证书没有私钥,所以我转换了证书并键入PKCS12,一个.pfx文件,使用SAME密钥密码作为密钥库密码,将其与keytool一起导入.jks文件,并在我的主类中设置系统属性。此证书绝对有效,因为它来自Comodo Positive SSL。但是当我尝试连接并发送数据时,我不断发现这些错误。
错误 http://pastebin.com/UWbjJLjn
有没有人想过为什么会发生这种情况。我的客户端日志说如果有帮助我的证书是有效的。
服务器
private static SSLSocket socket;
public PacketHandler() {
initListen();
}
private void initListen() {
System.setProperty("javax.net.ssl.keyStore", "/Users/181095/ting_keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
try {
SSLServerSocketFactory sf = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket serverSocket = (SSLServerSocket)sf.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();
System.out.print(Arrays.toString(socket.getSupportedProtocols()));
System.out.print( Arrays.toString(sf.getSupportedCipherSuites()));
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) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (Exception e) {
}
}
}
客户端
public static void Message(String args) {
System.setProperty("javax.net.debug", "all");
try
{
String host = "localhost";
int port = 3498;
InetAddress address = InetAddress.getByName(host);
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
socket = (SSLSocket)factory.createSocket(address, port);
System.out.print(Arrays.toString(socket.getSupportedProtocols()));
//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)
{
System.out.print(e.getCause());
}
}
}