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