Netty实现socket通讯实例

1.pom文件添加

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.39.Final</version>
        </dependency>

2.服务端处理器

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
 
public class SocketHandler extends SimpleChannelInboundHandler<String> {

    /**
     * 客户端发消息会触发
     */ 
    @Override
    public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println("========================[" + this.getIP(ctx) + "]收到消息:" + msg+"========================");
        ClientManager1.getInstance().handleMsg(this.getIP(ctx), "This is response");
    }
    /**
     * 客户端连接会触发
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //添加channel信息
        ClientManager1.getInstance().putChannel(this.getIP(ctx), ctx.channel());
        System.out.println("========================[" + this.getIP(ctx) + "]已连接========================");
    }

    /**
     * 客户端断开连接会触发
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        //删除失效的channel
        ClientManager1.getInstance().removeChannel(getIP(ctx));
        ctx.close();
        System.out.println("========================[" + this.getIP(ctx) + "]已断开========================");
    }

    /**
     * 发生异常触发
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable t) throws Exception {
        System.out.println("========================[" + this.getIP(ctx) + "]发生异常:" + t+"========================");
        ctx.close();
    }

    /**
     * 获取IP地址
     */
    private String getIP(ChannelHandlerContext ctx) {
        String socketString = ctx.channel().remoteAddress().toString();
        int index = socketString.indexOf(":");
        String ip = socketString.substring(1, index);
        return ip;
    }

}

3.服务端

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 *
 * @author Administrator
 */
public class Server {
    //端口号

    private int port;

    public Server(int port) {
        this.port = port;
    }

    //启动方法
    public void start() throws Exception {
        //负责接收客户端的连接的线程。线程数设置为1即可,netty处理链接事件默认为单线程,过度设置反而浪费cpu资源
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        //负责处理数据传输的工作线程。线程数默认为CPU核心数乘以2
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup);
            bootstrap.channel(NioServerSocketChannel.class);
            //在ServerChannelInitializer中初始化ChannelPipeline责任链,并添加到serverBootstrap中
            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { // (6)
                @Override
                public void initChannel(SocketChannel channel) throws Exception {
                    //添加编解码 
                    channel.pipeline().addLast("decoder", new StringDecoder());
                    channel.pipeline().addLast("encoder", new StringEncoder());
                    // 添加上自己的处理器
                    channel.pipeline().addLast("socketHandler", new SocketHandler()); 

                }
            });//标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度
            bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
            //是否启用心跳保活机制
            bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
            //将小的数据包包装成更大的帧进行传送,提高网络的负载
            bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
            //绑定端口后,开启监听
            ChannelFuture future = bootstrap.bind(port).sync();
            //等待服务监听端口关闭
            future.channel().closeFuture().sync();
        } finally {
            //释放资源
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}

4.服务端测试代码

    //服务端测试代码
    public static void main(String[] args) {
        try {
            int port = 8088;
            new Server(port).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

5.客户端

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;  

/**
 *
 * @author Administrator
 */
public class Client {
    //主机名/IP
    private String host;
    //端口号
    private int port;

    public Client(String host, int port) {
        this.host = host;
        this.port = port;
    }

    //启动方法
    public void start() throws Exception {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup);
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel channel) throws Exception {
                    channel.pipeline().addLast("decoder", new StringDecoder());
                    channel.pipeline().addLast("encoder", new StringEncoder());
                    channel.pipeline().addLast(new SimpleChannelInboundHandler<String>(){
                        @Override
                        protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
                            System.out.println("========================收到服务器消息:" + msg+"========================");
                        }
                    });
                }
            });

            //建立连接
            ChannelFuture future = bootstrap.connect(host, port).sync();
            //发送消息
            future.channel().writeAndFlush("This is Request");
            //等待服务监听端口关闭
            future.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }
}

6.客户端测试

    //客户端测试代码
    public static void main(String[] args) {
        try {
            String host = "192.168.100.38";
            int port = 8899;
            new Client(host, port).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用Netty实现一个客户端对三个服务端的Java代码示例: ```java import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; public class MultiServerClient { private List<Channel> channels = new ArrayList<>(); public void connect(String[] serverList) { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ClientHandler()); } }); for (String server : serverList) { String[] addr = server.split(":"); String host = addr[0]; int port = Integer.parseInt(addr[1]); ChannelFuture f = b.connect(new InetSocketAddress(host, port)).sync(); channels.add(f.channel()); } } catch (Exception e) { e.printStackTrace(); } } public void send(String message) { if (!channels.isEmpty()) { Channel channel = channels.get(0); // 这里选择第一个 Channel 实例发送数据 channel.writeAndFlush(message); } } public void close() { for (Channel channel : channels) { channel.close(); } channels.clear(); } private static class ClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 处理服务端响应的数据 } } public static void main(String[] args) { MultiServerClient client = new MultiServerClient(); String[] serverList = {"localhost:8080", "localhost:8081", "localhost:8082"}; client.connect(serverList); client.send("Hello, world!"); client.close(); } } ``` 在`connect()`方法中,循环遍历服务端列表,对于每个服务端,创建一个`InetSocketAddress`实例,然后调用`Bootstrap`的`connect()`方法连接服务端,并将返回的`ChannelFuture`实例中的`Channel`保存到`channels`列表中。 在`send()`方法中,从`channels`列表中选择一个`Channel`实例,通过`writeAndFlush()`方法发送数据。 在`main()`方法中,创建一个`MultiServerClient`实例,传入服务端列表,连接服务端,发送数据,然后关闭连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值