WebSocketException:无法获取原始套接字的输入流:握手失败

时间:2018-06-29 11:54:06

标签: java android ssl websocket wss

连接到安全Web套接字时出现异常

这是服务器代码...

 KeyStore keyStore = KeyStore.getInstance("BKS");ks.load(mActivity.getResources().openRawResource(R.raw.emm_truststore), STOREPASSWORD.toCharArray());

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
        kmf.init(ks, KEYPASSWORD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
        tmf.init(ks);
        SSLContext sslContext = null;
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        chatserver.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));

        chatserver.start();

这是客户端代码。...

 factory.setVerifyHostname(false);
            factory.setServerName("prince.com");
            WebSocket ws = null;
            try {
                ws = factory.createSocket("wss://localhost:55555");
            } catch (IOException e) {
                e.printStackTrace();
            }

            ws.addListener(new WebSocketListener() {
                @Override
                public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception {
                    Log.d(TAG, " onStateChanged ");
                }

                @Override
                public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
                    Log.d(TAG, " onConnected ");
                }

                @Override
                public void onConnectError(WebSocket websocket, WebSocketException cause) throws Exception {
                    Log.d(TAG, " onConnectError " + cause);
                }

                @Override
                public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
                    Log.d(TAG, " onDisconnected ");
                }


            });
            ws.connect();
            ws.sendText("Hellooooooooooooooooooooooooooooooooo.");
            System.out.println(" Sending Hello to Server ");

登录...

WebSocketException: Failed to get the input stream of the raw socket: Handshake failed
06-29 22:49:31.925 9698-9724/? W/System.err:     at com.neovisionaries.ws.client.WebSocket.openInputStream(WebSocket.java:3233)
06-29 22:49:31.926 9698-9724/? W/System.err:     at com.neovisionaries.ws.client.WebSocket.shakeHands(WebSocket.java:3191)
        at com.neovisionaries.ws.client.WebSocket.connect(WebSocket.java:2235)
06-29 22:49:31.926 9698-9724/? W/System.err:     at com.vvdn.prince.websocketclients.MainActivity$ServerThread.run(MainActivity.java:242)
06-29 22:49:31.926 9698-9724/? W/System.err: Caused by: javax.net.ssl.SSLHandshakeException: Handshake failed
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:276)
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket.waitForHandshake(ConscryptFileDescriptorSocket.java:468)
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket.getInputStream(ConscryptFileDescriptorSocket.java:431)
        at com.neovisionaries.ws.client.WebSocket.openInputStream(WebSocket.java:3226)
        ... 3 more
    Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7575234400: Failure in SSL library, usually a protocol error
    error:10000410:SSL routines:OPENSSL_internal:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/tls_record.cc:579 0x7575253060:0x00000001)
    error:1000009a:SSL routines:OPENSSL_internal:HANDSHAKE_FAILURE_ON_CLIENT_HELLO (external/boringssl/src/ssl/handshake_client.cc:893 0x7572f330ab:0x00000000)
        at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
06-29 22:49:31.927 9698-9724/? W/System.err:     at com.android.org.conscrypt.SslWrapper.doHandshake(SslWrapper.java:374)
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:217)
        ... 6 more

任何人都可以提出错误的建议吗?

我正在使用自签名证书来确保Web套接字安全(WSS)。 示例代码参考将不胜感激。

0 个答案:

没有答案