Search in sources :

Example 1 with HelloClientInitializer

use of test.org.server.netty.handle.HelloClientInitializer in project tech by ffyyhh995511.

the class HelloClient02 method main.

/**
 * @param args
 * @throws InterruptedException
 * @throws IOException
 */
public static void main(String[] args) throws InterruptedException, IOException {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new HelloClientInitializer());
        // Start the client.
        ChannelFuture f = b.connect(host, port).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // The connection is closed automatically on shutdown.
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) HelloClientInitializer(test.org.server.netty.handle.HelloClientInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 2 with HelloClientInitializer

use of test.org.server.netty.handle.HelloClientInitializer in project tech by ffyyhh995511.

the class HelloClient01 method main.

/**
 * @param args
 * @throws InterruptedException
 * @throws IOException
 */
public static void main(String[] args) throws InterruptedException, IOException {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new HelloClientInitializer());
        // 连接服务端
        Channel ch = b.connect(host, port).sync().channel();
        ch.writeAndFlush("i am 01" + "\r\n");
    // 控制台输入
    /*BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            for (;;) {
                String line = in.readLine();
                if (line == null) {
                    continue;
                }
                
                 * 向服务端发送在控制台输入的文本 并用"\r\n"结尾
                 * 之所以用\r\n结尾 是因为我们在handler中添加了 DelimiterBasedFrameDecoder 帧解码。
                 * 这个解码器是一个根据\n符号位分隔符的解码器。所以每条消息的最后必须加上\n否则无法识别和解码
                 * 
                ch.writeAndFlush(line + "\r\n");
            }*/
    } finally {
        // The connection is closed automatically on shutdown.
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) HelloClientInitializer(test.org.server.netty.handle.HelloClientInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 HelloClientInitializer (test.org.server.netty.handle.HelloClientInitializer)2 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1