Example usage for io.netty.util.internal SocketUtils addressByName

List of usage examples for io.netty.util.internal SocketUtils addressByName

Introduction

In this page you can find the example usage for io.netty.util.internal SocketUtils addressByName.

Prototype

public static InetAddress addressByName(final String hostname) throws UnknownHostException 

Source Link

Usage

From source file:com.cmz.sctp.multihoming.SctpMultiHomingEchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*from   w  w  w  .  ja  v a  2s  .com*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSctpChannel.class).option(SctpChannelOption.SCTP_NODELAY, true)
                .handler(new ChannelInitializer<SctpChannel>() {
                    @Override
                    public void initChannel(SctpChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                //                             new LoggingHandler(LogLevel.INFO),
                                new SctpEchoClientHandler());
                    }
                });

        InetSocketAddress localAddress = SocketUtils.socketAddress(CLIENT_PRIMARY_HOST, CLIENT_PORT);
        InetAddress localSecondaryAddress = SocketUtils.addressByName(CLIENT_SECONDARY_HOST);

        InetSocketAddress remoteAddress = SocketUtils.socketAddress(SERVER_REMOTE_HOST, SERVER_REMOTE_PORT);

        // Bind the client channel.
        ChannelFuture bindFuture = b.bind(localAddress).sync();

        // Get the underlying sctp channel
        SctpChannel channel = (SctpChannel) bindFuture.channel();

        // Bind the secondary address.
        // Please note that, bindAddress in the client channel should be done before connecting if you have not
        // enable Dynamic Address Configuration. See net.sctp.addip_enable kernel param
        channel.bindAddress(localSecondaryAddress).sync();

        // Finish connect
        ChannelFuture connectFuture = channel.connect(remoteAddress).sync();

        // Wait until the connection is closed.
        connectFuture.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:com.cmz.sctp.multihoming.SctpMultiHomingEchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from w  w  w .  ja v  a2s  . co m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioSctpServerChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SctpChannel>() {
                    @Override
                    public void initChannel(SctpChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                //                             new LoggingHandler(LogLevel.INFO),
                                new SctpEchoServerHandler());
                    }
                });

        InetSocketAddress localAddress = SocketUtils.socketAddress(SERVER_PRIMARY_HOST, SERVER_PORT);
        InetAddress localSecondaryAddress = SocketUtils.addressByName(SERVER_SECONDARY_HOST);

        // Bind the server to primary address.
        ChannelFuture bindFuture = b.bind(localAddress).sync();

        //Get the underlying sctp channel
        SctpServerChannel channel = (SctpServerChannel) bindFuture.channel();

        //Bind the secondary address
        ChannelFuture connectFuture = channel.bindAddress(localSecondaryAddress).sync();

        // Wait until the connection is closed.
        connectFuture.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:org.asynchttpclient.CustomRemoteAddressTest.java

License:Open Source License

@Test
public void getRootUrlWithCustomRemoteAddress() throws Throwable {
    withClient().run(client -> withServer(server).run(server -> {
        String url = server.getHttpUrl();
        server.enqueueOk();//from   w ww. j  a  v a  2 s .  co m
        RequestBuilder request = get(url).setAddress(SocketUtils.addressByName("localhost"));
        Response response = client.executeRequest(request, new AsyncCompletionHandlerAdapter()).get(TIMEOUT,
                SECONDS);
        assertEquals(response.getStatusCode(), 200);
    }));
}