首页 > 解决方案 > 连接安全 Websocket 时连接中止

问题描述

尝试从 Java Websocket 客户端应用程序连接 nodejs websocket 服务器时出现以下异常。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Connection closed by remote peer Code: -1 Reason:


Error:javax.net.ssl.SSLException: java.net.SocketException: Software caused connection abort: recv failed

以下是完整的代码。

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;


public class WebSocketTest extends WebSocketClient {
    static String CA_FILE = "ashwini.crt";
    public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream(CA_FILE);
    X509Certificate ca = (X509Certificate) CertificateFactory.getInstance("X.509")
            .generateCertificate(new BufferedInputStream(fis));
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, null);
    ks.setCertificateEntry(Integer.toString(1), ca);
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ks);

    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);
    context.init(null, tmf.getTrustManagers(), null);
    SSLSocketFactory factory = context
            .getSocketFactory();
    
    
    WebSocketTest c = new WebSocketTest(new URI(
        "wss://ashwinictc.ddns.net:9090")); // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts
    c.setSocketFactory(factory);
    try{
    c.connect();
    c.connectBlocking();
    }catch(Exception e){
        System.out.println(e.getMessage());
    }
    //c.connectBlocking();
  }

  public WebSocketTest(URI serverUri, Draft draft) {
    super(serverUri, draft);
  }

  public WebSocketTest(URI serverURI) {
    super(serverURI);
  }

  public WebSocketTest(URI serverUri, Map<String, String> httpHeaders) {
    super(serverUri, httpHeaders);
  }

  @Override
  public void onOpen(ServerHandshake handshakedata) {
    //send("Hello, it is me. Mario :)");
    System.out.println("opened connection");
    // if you plan to refuse connection based on ip or httpfields overload: onWebsocketHandshakeReceivedAsClient
  }

  @Override
  public void onMessage(String message) {
    System.out.println("received: " + message);
  }

  @Override
  public void onClose(int code, String reason, boolean remote) {
    // The codecodes are documented in class org.java_websocket.framing.CloseFrame
    System.out.println(
        "Connection closed by " + (remote ? "remote peer" : "us") + " Code: " + code + " Reason: "
            + reason);
  }

  @Override
  public void onError(Exception ex) {
    //ex.printStackTrace();
      System.out.println("Error:"+ex.toString());
    // if the error is fatal then onClose will be called additionally
  }

  

}


该错误在应用程序执行 20 分钟后出现。但是基于浏览器的 Javascript 应用程序工作正常。我需要一个网络应用程序的 Android 应用程序。

提前感谢您的帮助。

标签: javasslwebsocket

解决方案


推荐阅读