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