簡體   English   中英

如何在Java中將ISO消息對象從客戶端發送到Netty服務器

[英]How to send ISO message Object from client to Netty server in java

實際上,我可以使用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); } @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); }

您應該檢查writeChannelFuture告訴您什么。 它很可能會失敗,並帶有一個異常,它將告訴您更多為什么它不起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM