Netty创建Client

1、定义客户端

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.bootstrap.Bootstrap;  
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;  
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;  
import io.netty.channel.nio.NioEventLoopGroup;  
import io.netty.channel.socket.nio.NioSocketChannel;  



public class NettyClient implements Runnable{
	
	private static final Logger log = LoggerFactory.getLogger(NettyClient.class);
	
	public static byte[] b = new byte[] {};
	
	public static long time = 0;
	public static long maxTime = 0;
	public static int num = 0;

    private static volatile EventLoopGroup workerGroup;

    private String ip;

    private int port;

    public NettyClient(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    @SuppressWarnings("rawtypes")
	@Override
    public void run() {
        // 客户端的配置
        workerGroup= new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(Channel channel) throws Exception {
                            ChannelPipeline p = channel.pipeline();
                                    //具体的客户端实现操作类
                                    p.addLast(new SimpleClientHandler(bootstrap, ip, port));
                        }
                    });

            ChannelFuture future = null;
            /**
             *防止初次链接不上 每隔十秒重连一次
             */
            while (future == null || !future.isSuccess()) {
                try {
                    future = bootstrap.connect(ip, port).sync();
                } catch (Exception ex) {
                    log.warn("重连中。。。。");
                    Thread.sleep(10000);
                }
            }


            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //workerGroup.shutdownGracefully();
        }
    }



   

2、具体连接和数据处理

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 处理服务端返回的数据
 * 
 * @author Administrator
 *
 */
public class SimpleClientHandler extends ChannelInboundHandlerAdapter{
	
	private static Logger log = LoggerFactory.getLogger(SimpleClientHandler.class);

    /**
     *
     */
    private final Bootstrap bootstrap;

    /**
     * 交互channel
     */
    @SuppressWarnings("unused")
	private static Channel channel;

    /**
     * 服务端ip  重连时使用
     */
    private String ip;

    /**
     * 服务端端口
     */
    private int port;


    /**
     * 断线重连等待时间 /S
     */
    private final static int reconnectDelay = 1;

    public SimpleClientHandler(Bootstrap bootstrap, String ip, int port) {
        this.bootstrap = bootstrap;
        this.ip = ip;
        this.port = port;
    }

    /**
     * 客户端连接服务器后被调用
     * @param ctx
     * @throws InterruptedException 
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws InterruptedException {
        channel = ctx.channel();
        ctx.fireChannelActive();
        channelWrite();
    }

    /**
     * @param ctx
     * @DESCRIPTION: 服务端端终止连接 后触发此函数
     * @return: void
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        try {
        	ctx.fireChannelInactive();
        	log.warn("服务端终止了服务");
            super.channelInactive(ctx);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //断开连接后实现重连机制
        ctx.channel().eventLoop().schedule(() -> doConnect(), reconnectDelay, TimeUnit.SECONDS);
    }

    /**
     * 每隔十秒重连一次
     */
    private void doConnect() {
        ChannelFuture future = bootstrap.connect(new InetSocketAddress(ip, port));
        future.addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture f) throws Exception {
                if (f.isSuccess()) {
                	log.warn("");
                } else {
                	log.warn("Started Tcp Client Failed: ");
                    f.channel().eventLoop().schedule(() -> doConnect(), reconnectDelay, TimeUnit.SECONDS);
                }
            }
        });
    }

    /**
     * 从服务器接收到数据后调用
     * @param ctx
     * @param msg
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
    	log.warn("回写数据:" + msg);
		ByteBuf buf = (ByteBuf) msg;
		byte[] req = new byte[buf.readableBytes()];
		buf.readBytes(req);
		save();
    }
    
   

    /**
     * 发生异常时被调用
     * @param ctx
     * @param cause
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

        //cause.printStackTrace();
    	log.warn("服务端发生异常【{}】", cause.getMessage());
        ctx.close();
    }

    /**
     * 新启一个线程  轮询发送数据
     * @DESCRIPTION: 客户端给服务端发送消息
     * @return: void
     * @throws InterruptedException 
     */
    public void channelWrite() throws InterruptedException {
    	
    }
}

3、创建客户端

new NettyClient("9.6.5.7", 7777).run();//填写server的ip和端口

你可能感兴趣的:(java)