Netty 技术学习(二)

序言

漆黑双眸注视着深邃的夜空,坚毅的脸庞划过几滴泪水.........
所谓暗恋
就是你不经意的一瞥
我心尖一颤
一个小男孩在公园里看电影时,看到小丑打败了英雄,十分气愤,他拿起刀走向公园的小丑,小丑直摇头,将刀夺了过来,小男孩却往地上一趟,立刻引起了其他人的注意,小丑招来一顿毒打。小男孩起身挤在人群里开心的笑了,小丑也笑了

作者:blackking (喜欢自己写点东西,除了编程技术嗯,算是生活感悟吧)

正文:客户端编写嗯

一、客户端第一个类

package com.blackking.netty.testhandler;

import com.blackking.netty.secondtest.MyClientInitializer;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;


public class MyClient {
    public static void main(String[] args) throws Exception{
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try{
            Bootstrap bootstrap = new Bootstrap();
           /* 下面我们要通过反射来创建~~*/
            bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).
                    handler(new MyClientInitializer());
         /*   我们可以用null来占位~,下面我们来连接,并且将异常抛出去*/
            ChannelFuture channelFuture = bootstrap.connect("localhost",8899).sync();
            channelFuture.channel().close().sync();
            /*以上就是我们编写的详细逻辑~~*/
        }finally{
   /*         优雅关闭的方式*/
            eventLoopGroup.shutdownGracefully();
        }
    }
}

精华部分:记住、客户端和服务端肯定至少三个类

二、客户端第二个类【我们双眸掠过一抹坚毅,这是处理器类】

package com.blackking.netty.testhandler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.time.LocalDateTime;

/*其实和服务端也相差不大~~帅气*/
public class MyClienthandler extends SimpleChannelInboundHandler<Long> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {
         System.out.println(ctx.channel().remoteAddress());
       /*  上面代码我们把服务器端地址打印出来~~是服务器端的*/
         System.out.println("Client output"+msg);
/*         上面这个msg很明显就是服务端向客户端发送的数据信息~*/
         ctx.writeAndFlush("from Client" + LocalDateTime.now());
        /* 上面我们客户端向服务端发送一个数据信息~,这里用时间来举例*/
    }
/*    接下来还有一件事,我们要用另外一个方法exceptionCaught~*/

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(123456L);
        /*发送一个Long类型的,123456L*/
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        ctx.close();
    }
/*    以上我们代码就全部写完了~~*/
}

三、客户端第三个类【我们双眸掠过一抹坚毅,这是实现类】

package com.blackking.netty.testhandler;

import com.blackking.netty.secondtest.MyClienthandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;


/*我们也是让他去继承最常用的ChannelInitializer*/
/*ctrl+i来实现方法~*/
public class MyClientInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline Pipeline = ch.pipeline();
      /*  下面这个绕在里面,有点尴尬*/

        Pipeline.addLast(new MyByteToLongDecoder2());
        Pipeline.addLast(new MyLongToByteEncoder());
        Pipeline.addLast(new MyClienthandler());
       /* 这里这个null,我们先用来占据位置的,之后还要new一个新的~*/
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41987706/article/details/84473475