简体   繁体   English

如何使用Netty从服务器向客户端发送确认?

[英]How to send acknowledgment from server to client using netty?

Can any one explain how to send an acknowledgment from server to a device using netty . 谁能解释一下如何使用netty从服务器向设备发送确认。

I am using the following code: 我正在使用以下代码:

public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
            throws Exception {

    String msg = null;
    String IMIE = null;
    String[] str = null;
    String ack = null;
    StringBuffer sbs = new StringBuffer();
    ChannelBuffer buf = (ChannelBuffer) e.getMessage();

    while(buf.readable()) {
        sbs.append((char) buf.readByte());          
    }

    msg = sbs.toString();
    System.out.println(msg);


    byte[] request = new byte[] { 0x01 };
    e.getChannel().write(request,e.getChannel().getRemoteAddress());
    System.out.flush(); 

and am getting error 并出现错误

java.lang.IllegalArgumentException: unsupported message type: class[B java.lang.IllegalArgumentException:不支持的消息类型:class [B

Please, help me. 请帮我。

You need to wrap your byte array into an ChannelBuffer or add an Encoder that does this for you. 您需要将字节数组包装到ChannelBuffer或添加一个为您执行此操作的编码器。 So the simplest fix is this: 因此,最简单的解决方法是:

public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
        throws Exception {

    String msg = null;
    String IMIE = null;
    String[] str = null;
    String ack = null;
    ChannelBuffer buf = null;
    StringBuffer sbs = new StringBuffer();
    buf = (ChannelBuffer) e.getMessage();
    while(buf.readable()) {
        sbs.append((char) buf.readByte());
     }

    msg = sbs.toString();
    System.out.println(msg);


    byte[] request = new byte[] { 0x01 };
    e.getChannel().write(ChannelBuffers.wrappedBuffer(request));
    System.out.flush(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM