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.Bootstrap;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelInitializer;
21 import io.netty.channel.EventLoopGroup;
22 import io.netty.channel.nio.NioEventLoopGroup;
23 import io.netty.channel.sctp.SctpChannel;
24 import io.netty.channel.sctp.SctpChannelOption;
25 import io.netty.channel.sctp.nio.NioSctpChannel;
26 import io.netty.example.sctp.SctpEchoClientHandler;
27 import io.netty.util.internal.SocketUtils;
28
29 import java.net.InetAddress;
30 import java.net.InetSocketAddress;
31
32
33
34
35 public final class SctpMultiHomingEchoClient {
36
37 private static final String CLIENT_PRIMARY_HOST = System.getProperty("host.primary", "127.0.0.1");
38 private static final String CLIENT_SECONDARY_HOST = System.getProperty("host.secondary", "127.0.0.2");
39
40 private static final int CLIENT_PORT = Integer.parseInt(System.getProperty("port.local", "8008"));
41
42 private static final String SERVER_REMOTE_HOST = System.getProperty("host.remote", "127.0.0.1");
43 private static final int SERVER_REMOTE_PORT = Integer.parseInt(System.getProperty("port.remote", "8007"));
44
45 public static void main(String[] args) throws Exception {
46
47 EventLoopGroup group = new NioEventLoopGroup();
48 try {
49 Bootstrap b = new Bootstrap();
50 b.group(group)
51 .channel(NioSctpChannel.class)
52 .option(SctpChannelOption.SCTP_NODELAY, true)
53 .handler(new ChannelInitializer<SctpChannel>() {
54 @Override
55 public void initChannel(SctpChannel ch) throws Exception {
56 ch.pipeline().addLast(
57
58 new SctpEchoClientHandler());
59 }
60 });
61
62 InetSocketAddress localAddress = SocketUtils.socketAddress(CLIENT_PRIMARY_HOST, CLIENT_PORT);
63 InetAddress localSecondaryAddress = SocketUtils.addressByName(CLIENT_SECONDARY_HOST);
64
65 InetSocketAddress remoteAddress = SocketUtils.socketAddress(SERVER_REMOTE_HOST, SERVER_REMOTE_PORT);
66
67
68 ChannelFuture bindFuture = b.bind(localAddress).sync();
69
70
71 SctpChannel channel = (SctpChannel) bindFuture.channel();
72
73
74
75
76 channel.bindAddress(localSecondaryAddress).sync();
77
78
79 ChannelFuture connectFuture = channel.connect(remoteAddress).sync();
80
81
82 connectFuture.channel().closeFuture().sync();
83 } finally {
84
85 group.shutdownGracefully();
86 }
87 }
88 }