1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.securechat;
17
18 import io.netty.bootstrap.ServerBootstrap;
19 import io.netty.channel.EventLoopGroup;
20 import io.netty.channel.MultiThreadIoEventLoopGroup;
21 import io.netty.channel.nio.NioIoHandler;
22 import io.netty.channel.socket.nio.NioServerSocketChannel;
23 import io.netty.example.telnet.TelnetServer;
24 import io.netty.handler.logging.LogLevel;
25 import io.netty.handler.logging.LoggingHandler;
26 import io.netty.handler.ssl.SslContext;
27 import io.netty.handler.ssl.SslContextBuilder;
28 import io.netty.handler.ssl.util.SelfSignedCertificate;
29
30
31
32
33 public final class SecureChatServer {
34
35 static final int PORT = Integer.parseInt(System.getProperty("port", "8992"));
36
37 public static void main(String[] args) throws Exception {
38 SelfSignedCertificate ssc = new SelfSignedCertificate();
39 SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
40 .build();
41
42 EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
43 EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
44 try {
45 ServerBootstrap b = new ServerBootstrap();
46 b.group(bossGroup, workerGroup)
47 .channel(NioServerSocketChannel.class)
48 .handler(new LoggingHandler(LogLevel.INFO))
49 .childHandler(new SecureChatServerInitializer(sslCtx));
50
51 b.bind(PORT).sync().channel().closeFuture().sync();
52 } finally {
53 bossGroup.shutdownGracefully();
54 workerGroup.shutdownGracefully();
55 }
56 }
57 }