我正在制作一个 Android 应用程序,我正在尝试在两个对等方之间设置 SSL 套接字。启动客户端和服务器时,我收到以下错误消息:读取错误:ssl=0x707d0912c8:系统调用期间的 I/O 错误,软件导致连接中止。有谁知道问题可能是什么?
2021-03-15 12:38:26.820 14794-19150/com.example.testaware D/LOG-Test-Aware-App-Server: Read error: ssl=0x707d0912c8: I/O error during system call, Software caused connection abort
2021-03-15 12:38:26.821 14794-19150/com.example.testaware W/System.err: javax.net.ssl.SSLException: Read error: ssl=0x707d0912c8: I/O error during system call, Software caused connection abort
2021-03-15 12:38:26.821 699-20377/? I/netd: interfaceSetCfg() <24.82ms>
2021-03-15 12:38:26.821 14794-19150/com.example.testaware W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
2021-03-15 12:38:26.821 14794-19150/com.example.testaware W/System.err: at com.android.org.conscrypt.NativeSsl.read(NativeSsl.java:411)
2021-03-15 12:38:26.821 14794-19150/com.example.testaware W/System.err: at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:583)
2021-03-15 12:38:26.821 14794-19150/com.example.testaware W/System.err: at java.io.BufferedInputStream.fill(BufferedInputStream.java:248)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at java.io.BufferedInputStream.read1(BufferedInputStream.java:288)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at java.io.BufferedInputStream.read(BufferedInputStream.java:347)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2454)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2470)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2947)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:858)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at java.io.ObjectInputStream.<init>(ObjectInputStream.java:353)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at com.example.testaware.AppServer.lambda$new$0$AppServer(AppServer.java:63)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at com.example.testaware.-$$Lambda$AppServer$ooJGZGiLsVQGTcSkPsN_XmX4et8.run(Unknown Source:6)
2021-03-15 12:38:26.822 14794-19150/com.example.testaware W/System.err: at java.lang.Thread.run(Thread.java:919)
客户端的代码是:
public AppClient(KeyPair keyPair, SSLContext sslContext){
this.keyPair = keyPair;
this.sslContext = sslContext;
Thread thread = new Thread(this);
thread.start();
}
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void run() {
running = true;
sslSocket = null;
this.inet6Address = MainActivity.getPeerIpv6();
try {
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
while(running){
sslSocket = (SSLSocket) socketFactory.createSocket(inet6Address, Constants.SERVER_PORT);
inputStream = new ObjectInputStream(new BufferedInputStream(sslSocket.getInputStream()));
outputStream = new DataOutputStream(sslSocket.getOutputStream());
outputStream.writeUTF("clientHello");
outputStream.flush();
//TODO: send client hello message
while(running){
if (inputStream != null){
String strMessageFromClient = (String) inputStream.readUTF();
ChatActivity.setChat(strMessageFromClient);
}
}
}
} catch (IOException e) {
e.printStackTrace();
if(sslSocket != null){
try {
sslSocket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
@RequiresApi(api = Build.VERSION_CODES.Q)
public boolean sendMessage(String message){
if(outputStream == null){
return false;
}
Runnable sendMessageRunnable = () -> {
try {
outputStream.writeUTF(message);
outputStream.flush();
MessageListItem chatMsg = new MessageListItem(message, ChatActivity.getLocalIp());
ChatActivity.messageList.add(chatMsg);
} catch (IOException e) {
e.printStackTrace();
}
};
sendService.submit(sendMessageRunnable);
return true;
}
和服务器:
public AppServer(SSLContext serverSSLContext, int serverPort){
running = true;
Runnable serverTask = () -> {
running = true;
try {
SSLServerSocket serverSocket = (SSLServerSocket) serverSSLContext.getServerSocketFactory().createServerSocket(serverPort);
serverSocket.setNeedClientAuth(true);
while (running) {
SSLSocket sslClientSocket = (SSLSocket) serverSocket.accept();
inputStream = new ObjectInputStream(new BufferedInputStream(sslClientSocket.getInputStream()));
while(running){
if (inputStream != null){
String strMessageFromClient = (String) inputStream.readUTF();
MessageListItem chatMsg = new MessageListItem(strMessageFromClient, "ipv6_other_user");
messageList.add(chatMsg);
}
}
}
} catch (IOException e) {
Log.d(LOG, Objects.requireNonNull(e.getMessage()));
e.printStackTrace();
}
};
Thread serverThread = new Thread(serverTask);
serverThread.start();
}