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