我有一個server.java文件:如何在端口進行SSL操作80
import javax.net.ssl.*;
import java.io.*;
//*****************
public class Server
{
public static void main(String[] args)
{
try {
SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket =(SSLServerSocket)sslserversocketfactory.createServerSocket(80);
SSLSocket sslsocket = (SSLSocket)
sslserversocket.accept();
InputStream is = sslsocket.getInputStream();
InputStreamReader isr = new
InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String string = null;
while ((string = br.readLine()) != null)
{
System.out.println(string);
System.out.flush();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
,並作爲Client.java文件:
import javax.net.ssl.*;
import java.io.*;
//*****************
public class Client
{
public static void main(String[] args)
{
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("127.0.0.1", 80);
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = sslsocket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String string = null;
while ((string = br.readLine()) != null)
{
bw.write(string + '\n');
bw.flush();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
也是我創造了我自己的證書這樣的:
C:\Temp>keytool -genkey -keystore myCertificate -keyalg RSA
Enter keystore password: abcdefg
Re-enter new password: abcdefg
What is your first and last name?
[Unknown]: first last
What is the name of your organizational unit?
[Unknown]: cs
What is the name of your organization?
[Unknown]: stackoverflow
What is the name of your City or Locality?
[Unknown]: NYC
What is the name of your State or Province?
[Unknown]: NY
What is the two-letter country code for this unit?
[Unknown]: us
Is CN=first last, OU=cs, O=stackoverflow, L=NYC, ST=NY, C=us correct?
[no]: yes
Enter key password for <mykey>
(RETURN if same as keystore password):
但無論我做什麼我不斷收到錯誤爲:
main, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during
handshake
main, SEND TLSv1 ALERT: fatal, description = handshake_failure
main, WRITE: TLSv1 Alert, length = 2
main, called closeSocket()
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
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 sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
at sun.nio.cs.StreamEncoder.flush(Unknown Source)
at java.io.OutputStreamWriter.flush(Unknown Source)
at java.io.BufferedWriter.flush(Unknown Source)
at Client.main(Client.java:25)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(Unknown Source)
... 11 more
我的問題是,如果我將端口從更改爲其他內容,代碼工作得很好,但我必須專門處理此端口本身。證書和這兩個java文件都在相同的文件夾。你們能幫我找出在80端口上執行SSL操作的方法嗎?
我知道它沒有配置爲@Java1,但即時通訊要求一種方式來實現這種方式,這種操作可以運行 – user9517536248
@BalusC請你幫我解決這個問題 – user9517536248