0

I am following this link for running socket and sslsocket on same port. Please check below code for same port.

private void startServer(int port) {
    try {
        ServerSocketFactory ssf = ServerSocketFactory.getDefault();
        ServerSocket serverSocket = ssf.createServerSocket();
        serverSocket.bind(new InetSocketAddress("127.0.0.1", port));
        serverSocket.setReuseAddress(true);

        SSLContext sslContext = SslUtil.createSSLContext(this);
        SSLSocketFactory sslSf = sslContext.getSocketFactory();
        while (true) {
            try {
                Socket client = serverSocket.accept();
                SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(client, client.getInetAddress().getHostAddress(),
                        client.getPort(), false);
                sslSocket.setUseClientMode(false);

                boolean isSecure = false;
                String _url = "http://127.0.0.1:" + port + "/";
                String protocol = sslSocket.getSession().getProtocol();
                if (protocol != null && protocol.contains("TLS")) {
                    _url = "https://127.0.0.1:" + port + "/";
                    isSecure = true;
                }
                if (isSecure) {
                    executorService.execute(new HttpProcessor(sslSocket, _url, ctx));
                } else {
                    executorService.execute(new HttpProcessor(client, _url, ctx));
                }
            } catch (Exception ex) {
                serverSocket.close();
                Log.e("EX:", ex.toString());
                findPort();
                break;
            }
        }
    } catch (Exception e) {
        Log.e("Error:p1:", e.toString());
    }

}

This code is working fine up to android version 6.0 (Marshmallow). But when I running this code on android 7.0 and above, Https working fine but Http not working.

This line "String protocol = sslSocket.getSession().getProtocol();" in connection close while called http request.

Is there any other way to achieve this?

how to find client request is http or https sslSocket in android?

Thanks

Jigar Shekh
  • 2,800
  • 6
  • 29
  • 54
  • `This line "String protocol = sslSocket.getSession().getProtocol();" in connection close while called http request.` ???? Dont understand. What is happening? – greenapps Jun 01 '18 at 07:38
  • @greenapps When call http://127.0.0.1:8004 from browser then browser show message "connection close". If I comment this line and check its working fine. – Jigar Shekh Jun 01 '18 at 09:28
  • how to find client request is http or https sslSocket in android? – Jigar Shekh Jun 02 '18 at 04:53

0 Answers0