Netty(八) 多客户端连接例子

Netty(八) 多客户端连接例子

netty学习目录
一、Netty(一) NIO例子
二、Netty(二) netty服务端
三、Netty(三) Netty客户端+服务端
四、Netty(四) 简化版Netty源码
五、Netty(五)Netty5.x服务端
六、Netty(六) Netty Http 服务器例子
七、Netty(七) Netty服务端+客户端代码
八、Netty(八) Netty多客户端连接例子
九、Netty(九) Netty会话清除
十、Netty(十) Netty自定义编码器解码器
十一、Netty(十一) Netty对象传输

多客户端连接,支持重连

package com.zqw.netty5x;


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

public class MutiClient1 {
    private Bootstrap bootstrap = new Bootstrap();
    private final AtomicInteger index = new AtomicInteger();
    //会话对象组
    private List channels = new ArrayList<>();
    public void init(int count){
        bootstrap.group(new NioEventLoopGroup());
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.handler(new ChannelInitializer() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new StringEncoder());
                ch.pipeline().addLast(new StringDecoder());
                ch.pipeline().addLast(new ClientHandler1());
            }
        });
        for(int i=0; inew InetSocketAddress("192.168.1.4", 7777)).channel();
            channels.add(channel);
        }

    }
    public Channel next(){
        //获取可用的channel
        return getChannel(0);
    }
    private Channel getChannel(int count){
        Channel channel = channels.get(Math.abs(this.index.getAndIncrement()%channels.size()));
        if(count >= channels.size()){
            throw new RuntimeException("没有足够的channel");
        }
        if(!channel.isActive()){
            //重连
            reconnect(channel);
            //尝试获取下一个channel
            return getChannel(++count);
        }
        return channel;
    }

    private void reconnect(Channel channel) {
        synchronized (channel){
            int index = channels.indexOf(channel);
            channel =  bootstrap
                    .connect(new InetSocketAddress("192.168.1.4", 7777)).channel();
            channels.set(index, channel);
        }
    }

    public static void main(String[] args) {
        MutiClient1 mutiClient = new MutiClient1();
        mutiClient.init(10);
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入:");
            String str = scanner.next();
            try {
                Channel channel = mutiClient.next();
                channel.writeAndFlush(str);
            }catch (Exception e){
                e.printStackTrace();
            }

        }

    }

}
package com.zqw.netty5x;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandlerInvoker;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.EventExecutorGroup;

public class ClientHandler1 extends SimpleChannelInboundHandler<String> {
    @Override
    protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg);
    }
}

你可能感兴趣的