Search in sources :

Example 1 with ClientChannelInitializer

use of org.ko.netty.t3.initializer.ClientChannelInitializer in project tutorials-java by Artister.

the class HelloWorldClient method start.

public void start() {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ClientChannelInitializer());
    try {
        ChannelFuture future = bootstrap.connect(address, port).sync();
        future.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ClientChannelInitializer(org.ko.netty.t3.initializer.ClientChannelInitializer) ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 2 with ClientChannelInitializer

use of org.ko.netty.t3.initializer.ClientChannelInitializer in project tutorials-java by Artister.

the class HelloWorldClient method start.

public void start() {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ClientChannelInitializer());
    try {
        Channel channel = bootstrap.connect(address, port).sync().channel();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        for (; ; ) {
            String msg = reader.readLine();
            if (msg == null) {
                continue;
            }
            channel.writeAndFlush(msg + "\r\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ClientChannelInitializer(org.ko.netty.t1.initializer.ClientChannelInitializer) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) InputStreamReader(java.io.InputStreamReader) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) BufferedReader(java.io.BufferedReader) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 3 with ClientChannelInitializer

use of org.ko.netty.t3.initializer.ClientChannelInitializer in project tutorials-java by Artister.

the class HwClient method start.

public void start() {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ClientChannelInitializer());
    try {
        ChannelFuture future = bootstrap.connect(address, port).sync();
        future.channel().writeAndFlush("Hello Netty Server ,I am a common client");
        future.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ClientChannelInitializer(org.ko.netty.t1.initializer.ClientChannelInitializer) ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)3 EventLoopGroup (io.netty.channel.EventLoopGroup)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 ChannelFuture (io.netty.channel.ChannelFuture)2 ClientChannelInitializer (org.ko.netty.t1.initializer.ClientChannelInitializer)2 Channel (io.netty.channel.Channel)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 ClientChannelInitializer (org.ko.netty.t3.initializer.ClientChannelInitializer)1