javasocketssslchannelsocketchannel

Using an SSLContext to create a SocketChannel


I'm looking to implement NIO for a SSL socket. However, every example that I've found, uses SocketChannel.open() to get the socket channel.

From a SSLContext, how can I get a SocketChannel?


Solution

  • This is unfortunately not very easy. You can't directly create a SocketChannel that automatically uses SSL from an SSLContext.

    From the SSLContext, you can create an SSLEngine, and you'll have to call it appropriately to handle the SSL handshake. This isn't a matter of a few lines of code.

    I have an sslclient example on GitHub with a class SSLSocketChannel which shows how to do this.