1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.sctp.multihoming;
17
18 import io.netty.bootstrap.ServerBootstrap;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelInitializer;
21 import io.netty.channel.ChannelOption;
22 import io.netty.channel.EventLoopGroup;
23 import io.netty.channel.MultiThreadIoEventLoopGroup;
24 import io.netty.channel.nio.NioIoHandler;
25 import io.netty.channel.sctp.SctpChannel;
26 import io.netty.channel.sctp.SctpServerChannel;
27 import io.netty.channel.sctp.nio.NioSctpServerChannel;
28 import io.netty.example.sctp.SctpEchoServerHandler;
29 import io.netty.handler.logging.LogLevel;
30 import io.netty.handler.logging.LoggingHandler;
31 import io.netty.util.internal.SocketUtils;
32
33 import java.net.InetAddress;
34 import java.net.InetSocketAddress;
35
36
37
38
39 public final class SctpMultiHomingEchoServer {
40
41 private static final String SERVER_PRIMARY_HOST = System.getProperty("host.primary", "127.0.0.1");
42 private static final String SERVER_SECONDARY_HOST = System.getProperty("host.secondary", "127.0.0.2");
43
44 private static final int SERVER_PORT = Integer.parseInt(System.getProperty("port", "8007"));
45
46 public static void main(String[] args) throws Exception {
47
48 EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
49 EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
50 try {
51 ServerBootstrap b = new ServerBootstrap();
52 b.group(bossGroup, workerGroup)
53 .channel(NioSctpServerChannel.class)
54 .option(ChannelOption.SO_BACKLOG, 100)
55 .handler(new LoggingHandler(LogLevel.INFO))
56 .childHandler(new ChannelInitializer<SctpChannel>() {
57 @Override
58 public void initChannel(SctpChannel ch) throws Exception {
59 ch.pipeline().addLast(
60
61 new SctpEchoServerHandler());
62 }
63 });
64
65 InetSocketAddress localAddress = SocketUtils.socketAddress(SERVER_PRIMARY_HOST, SERVER_PORT);
66 InetAddress localSecondaryAddress = SocketUtils.addressByName(SERVER_SECONDARY_HOST);
67
68
69 ChannelFuture bindFuture = b.bind(localAddress).sync();
70
71
72 SctpServerChannel channel = (SctpServerChannel) bindFuture.channel();
73
74
75 ChannelFuture connectFuture = channel.bindAddress(localSecondaryAddress).sync();
76
77
78 connectFuture.channel().closeFuture().sync();
79 } finally {
80
81 bossGroup.shutdownGracefully();
82 workerGroup.shutdownGracefully();
83 }
84 }
85 }