服务器不接收消息

时间:2016-11-18 18:36:34

标签: java netty

我目前正在尝试学习netty,但没有任何作用......): 不知何故,服务器没有从客户端收到任何消息,我不知道为什么。请记住,我从未在netty中构建一个有效的应用程序,所以我绝对没有经验。如果有人可以帮助我,我会很高兴。 我发布了很多代码,我很抱歉,但我不知道如何使这个问题变小。

所以,这是我的代码:

    new Server().run();
    Client client = new Client();
    client.run();

    while(true){
        System.out.println("sending message to server...");
        client.sendMessage(new PacketPing(System.currentTimeMillis()));
        Thread.sleep(1000);
    }

这是我的客户:

private Channel channel;
@Override public void run() {
    ChannelFuture future = new Bootstrap().
            group(new NioEventLoopGroup()).
            channel(NioSocketChannel.class).
            handler(new ChannelInitializer<SocketChannel>() {
                @Override protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(
                            new ObjectEncoder(),
                            new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                            new ChannelInboundHandlerAdapter() {
                                @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    System.out.println("client received message!");
                                }
                            });
                }
            }).connect("localhost", 40401);
    try {
        future.get();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    this.channel = future.channel();
}
public void sendMessage(Object msg) {
    channel.writeAndFlush(msg);
}

这是我的服务器:

@Override public void run() {
    new ServerBootstrap().
            group(new NioEventLoopGroup(1), new NioEventLoopGroup()).
            channel(NioServerSocketChannel.class).
            handler(new LoggingHandler(LogLevel.INFO)).
            childHandler(new ChannelInitializer<SocketChannel>() {
                @Override protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(
                            new ObjectEncoder(),
                            new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                            new ChannelInboundHandlerAdapter() {
                                @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    System.out.println("server received a message!");
                                }
                            });
                }
            }).bind(40401);
}

我认为我缺少一些基本的理解,我不知道从哪里开始调试。代码非常简单,所以每个熟悉netty的人都应该很容易回答这个问题(: 谢谢@everybody!

控制台:

Nov 18, 2016 7:47:58 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFORMATION: [id: 0x52b5b879] REGISTERED
Nov 18, 2016 7:47:58 PM io.netty.handler.logging.LoggingHandler bind
INFORMATION: [id: 0x52b5b879] BIND: 0.0.0.0/0.0.0.0:40401
Nov 18, 2016 7:47:58 PM io.netty.handler.logging.LoggingHandler channelActive
INFORMATION: [id: 0x52b5b879, L:/0:0:0:0:0:0:0:0:40401] ACTIVE
Nov 18, 2016 7:47:58 PM io.netty.handler.logging.LoggingHandler channelRead
INFORMATION: [id: 0x52b5b879, L:/0:0:0:0:0:0:0:0:40401] RECEIVED: [id: 0xdda43b4c, L:/127.0.0.1:40401 - R:/127.0.0.1:56421]
sending message to server...
sending message to server...
sending message to server...
sending message to server...
sending message to server...
sending message to server...

1 个答案:

答案 0 :(得分:0)

修复了这个错误,感谢Ferrybig。我的数据包不可序列化。