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 group = new NioEventLoopGroup();
48 try {
49 ServerBootstrap b = new ServerBootstrap();
50 b.group(group)
51 .channel(NioSctpServerChannel.class)
52 .option(ChannelOption.SO_BACKLOG, 100)
53 .handler(new LoggingHandler(LogLevel.INFO))
54 .childHandler(new ChannelInitializer<SctpChannel>() {
55 @Override
56 public void initChannel(SctpChannel ch) throws Exception {
57 ch.pipeline().addLast(
58
59 new SctpEchoServerHandler());
60 }
61 });
62
63 InetSocketAddress localAddress = SocketUtils.socketAddress(SERVER_PRIMARY_HOST, SERVER_PORT);
64 InetAddress localSecondaryAddress = SocketUtils.addressByName(SERVER_SECONDARY_HOST);
65
66
67 ChannelFuture bindFuture = b.bind(localAddress).sync();
68
69
70 SctpServerChannel channel = (SctpServerChannel) bindFuture.channel();
71
72
73 ChannelFuture connectFuture = channel.bindAddress(localSecondaryAddress).sync();
74
75
76 connectFuture.channel().closeFuture().sync();
77 } finally {
78
79 group.shutdownGracefully();
80 }
81 }
82 }