相关文章推荐
不拘小节的皮带  ·  AIX Open Source: ...·  1 年前    · 
烦恼的紫菜汤  ·  ERROR ...·  1 年前    · 

netty tls client example

Netty是一个异步事件驱动的Java网络框架。下面是一个使用Netty的TLS客户端示例代码。

public class TlsClient {
    public static void main(String[] args) throws Exception {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(workerGroup);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.SO_KEEPALIVE, true);
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new SslHandler(createSslContext().newEngine(ch.alloc())));
            // Start the client.
            ChannelFuture f = b.connect("localhost", 8443).sync();
            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
    private static SslContext createSslContext() throws Exception {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        return sslCtx;

这仅仅是一个简单的例子,你可以根据自己的需要更改代码。

  •