Is it possible to change plain socket to SSLSocket?

后端 未结 4 1869
一整个雨季
一整个雨季 2020-11-27 19:48

There is a plain socket server listening on port 12345;

ServerSocket s = new ServerSocket(12345);

What I want to know is that

4条回答
  •  忘掉有多难
    2020-11-27 20:32

    It is not possible to serve both http and https on the same port. However, it should in theory be possible to upgrade an existing http connection to use TLS if both the client and server supports it, see RFC 2817. The SSLSocketFactory class has a createSocket() function which can be used to upgrade an existing socket to SSL/TLS.

    Disclaimer: I have not attempted to do this, and implementing RFC 2817 is likely non-trivial.

    Edit: Apparently I was wrong, like Bruno wrote it is possible to serve http and https on the same port using a technology called port unification, which uses the first few bytes received to detect the protocol. There is an example of this included with the Netty (JBoss) framework.

提交回复
热议问题