为什么总是在服务器和客户端中调用channelActive方法-Netty

问题描述 投票:0回答:1

我正在使用以下工作示例。这可能是“代码墙”,但是问题很简单:为什么总是调用方法channelActive?如果您不想阅读代码,也可以在此处找到示例。 Netty Server Client Pojo example

NettyClient:

public class NettyClient {

    private String host;
    private int port = 5000;

    public NettyClient(String host) {
        this.host = host;
    }

    public void send() throws InterruptedException {

        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
                protected void initChannel(SocketChannel ch) throws Exception {

                    ChannelPipeline p = ch.pipeline();
                    p.addLast(new ObjectEncoder());
                    p.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
                    p.addLast(new NettyClientHandler());
                }
            });

            ChannelFuture future = b.connect(host, port).sync();

            future.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }

}

NettyClientHandler

public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    @Inject
    private DeviceMessage deviceMessage;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // Send the message to JLoggerServer - contains deviceName, slider0, slider1, slider2, slider3, slider4, slider5 
        System.out.println("Channel active - client");
        ctx.write(deviceMessage);

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // Read the response from JLoggerServer - Contains deviceName, adc0, adc1, adc2, adc3, adc4, adc5
        DeviceMessage responseMessage = (DeviceMessage) msg;
        deviceMessage.setAdcValues(responseMessage.getAdcValues());
        System.out.println("read from server");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}

NettyServer

public class NettyServer extends Thread{

    private Logger logger = LoggerFactory.getLogger(getClass());

    private int port = 5000;

    public NettyServer() {
    }

    @Override
    public void run() {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, 1024)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {

                    ChannelPipeline p = ch.pipeline();
                    p.addLast(new ObjectEncoder());
                    p.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
                    p.addLast(new NettyServerHandler());
                }
            });

            // Bind and start to accept incoming connections.
            ChannelFuture f = b.bind(port).sync(); // (7)

            logger.info("server bind port:{}", port);

            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            logger.info(e.getMessage());
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

}

最后是NettyServerHandler。

public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private Map<String, SerialPort> allDevices;

    /**
     * This will be called when we send something from client to server and server
     * will respond
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("Read from client");

        // Get message
        DeviceMessage deviceMessage = (DeviceMessage) msg;
        System.out.println(deviceMessage.getDevicename());
        SerialPort serialPort = allDevices.get(deviceMessage.getDevicename());
        if (serialPort == null)
            return;

        // Create TXData memory with twice of length as values array and insert its values
        byte[] TXData = new byte[deviceMessage.getSliderValues().length * 2];
        for (int i = 0; i < TXData.length; i += 2) {
            TXData[i] = (byte) (deviceMessage.getSliderValues()[i] & 0xff);
            TXData[i + 1] = (byte) ((deviceMessage.getSliderValues()[i] >> 8) & 0xff);

            System.out.println("Slider" + i + ": " + deviceMessage.getSliderValues()[i]);
        }

        // Write to JLoggerDevice
        serialPort.writeBytes(TXData, TXData.length);

        // Create RXData memory with twice of length as values array and read the ADC values from JLoggerDevice
        byte[] RXData = new byte[deviceMessage.getAdcValues().length * 2];
        int receivedBytes = serialPort.bytesAvailable();
        while (receivedBytes < RXData.length)
            receivedBytes = serialPort.bytesAvailable(); // We need to have values.length * 2 bytes to pass
        serialPort.readBytes(RXData, RXData.length);

        // Create them to short
        for (int i = 0; i < RXData.length / 2; i++) {
            deviceMessage.getAdcValues()[i] = (short) ((RXData[i + 1] << 8) + (RXData[i] & 0xFF));
            System.out.println("Adc" + i + ": " + deviceMessage.getAdcValues()[i]);
        }

        // Write back message to the client
        ctx.write(deviceMessage);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        logger.info("Channel active - server");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.error("server caught exception", cause);
        ctx.close();
    }

}

问题是,当我启动NettyServer时。

        // Start the Netty server
        try {
            new NettyServer().start();
            logger.info("Starting netty server");
        } catch (Exception e) {
            logger.info(e.getMessage());
        }

当我尝试从客户端->服务器写入时,我总是得到这个作为putput。

客户端:

Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client
Channel active - client

和服务器端:

2019-09-25 20:43:34.688  INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:34.984  INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:35.268  INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:35.571  INFO 8639 --- [ntLoopGroup-3-1] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:35.855  INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:36.137  INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:36.468  INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:36.777  INFO 8639 --- [ntLoopGroup-3-1] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:37.072  INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:37.364  INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:37.648  INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:37.930  INFO 8639 --- [ntLoopGroup-3-1] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:38.214  INFO 8639 --- [ntLoopGroup-3-2] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:38.503  INFO 8639 --- [ntLoopGroup-3-3] s.d.J.netty.NettyServerHandler           : Channel active - server
2019-09-25 20:43:38.797  INFO 8639 --- [ntLoopGroup-3-4] s.d.J.netty.NettyServerHandler 

我们至少可以说客户端已连接到服务器。 :)

java sockets netty
1个回答
0
投票
每次接受新连接时,都会调用

channelActive。我怀疑您在从客户端写入服务器时创建了新连接。

© www.soinside.com 2019 - 2024. All rights reserved.