如何在java中从客户端向Netty服务器发送ISO消息对象

时间:2017-08-27 13:13:51

标签: java netty

实际上我可以使用netty发送和接收String对象,但是我无法发送任何像ISO消息的对象,我找不到原因。 这是客户端的启动方法

public void start() {
    EventLoopGroup group = new NioEventLoopGroup();

    try {
        Bootstrap bootstrap = new Bootstrap().group(group)
                .channel(NioSocketChannel.class)
                .handler(new ClientAdapterInitializer());

        Channel channel = bootstrap.connect(server, port).sync().channel();

        channel.write("Hi\n");


        IsoMessage o = new IsoMessage();
        o.setType(0200);
        o.setBinary(true);
        Teacher t = new Teacher(1, "hhhhh");
        //ObjectEncoder encoder = new ObjectEncoder();
        o.setField(3, new IsoValue(IsoType.BINARY, group, 1100));
        channel.write(t);
        channel.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        //  group.shutdownGracefully();
    }
}

并在服务器@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { IsoMessage is=(IsoMessage) msg; System.out.println(""+is.getType()); //ByteBuf byteBuf = (ByteBuf) is; //System.out.println(""+byteBuf.toString()); //logger.info("message : {} " + //byteBuf.toString(Charset.defaultCharset())); channels.writeAndFlush(msg); }

1 个答案:

答案 0 :(得分:0)

您应该检查ChannelFuture的{​​{1}}告诉您的内容。它很可能会失败,但有一个例外情况会告诉你更多为什么它不起作用