Come lettura / scrittura con Netty quando dall'altra parte sta usando readUTF / writeUTF?

StackOverflow https://stackoverflow.com/questions/4787286

  •  24-10-2019
  •  | 
  •  

Domanda

Sto cercando di comunicare con un server che utilizza DataInputStream.readUTF e DataOutputStream.writeUTF.

Ho fatto il solito codice di bootstrap di impostare il mio cliente, e impostare le seguenti pipelinefactory

    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                    new LengthFieldBasedFrameDecoder(65536, 0, 2),
                    new StringDecoder(CharsetUtil.UTF_8),
                    new StringEncoder(CharsetUtil.UTF_8),
                    new MyClientHandler());
        }
    });

in MyClientHandler che si estende SimpleChannelUpstreamHandler, ho il seguente:

    boolean sent = false; //is this thread safe?

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        logger.log(Level.INFO, e.getMessage().toString());
        e.getChannel().write("Hello over there!");

        if(!sent){
             //do something and set sent
        }
    }

Sono riuscito a ricevere messaggi da server con successo, ma il server non riceve la mia "là sopra ciao" messaggio. Non so cosa avrei potuto trascurato.

Si noti inoltre l'SENT booleano, posso aggiungere tali campi e lavorare con loro senza threading preoccupazioni?

È stato utile?

Soluzione

  • Sono riuscito a ricevere messaggi da server con successo, ma il server non riceve la mia "là sopra ciao" messaggio. Non so cosa avrei potuto trascurato.

Perché il messaggio dal server è stato in grado di essere ricevuti utilizzando LengthFieldBasedFrameDecoder, il messaggio ha un campo di lunghezza.

 +--------+----------+
 | Length | Message  |
 +--------+----------+

Pertanto, v'è una possibilità che il server si aspetta il messaggio ricevuto ha la lunghezza campo. Come se il campo lunghezza è scritto come segue?

 +--------+---------------------+
 | 0x0011 | "Hello over there!" |
 +--------+---------------------+

[campione]

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    logger.log(Level.INFO, e.getMessage().toString());
    byte[] message = "Hello over there!".getBytes("UTF-8");

    ChannelBuffer buf = ChannelBuffers.buffer(message.length + 2);
    buf.clear();
    short len = (short)message.length;
    buf.writeShort(len);
    buf.writeBytes(message);
    e.getChannel().write(buf);

    if(!sent){
        //do something and set sent
    }
}
  • Si noti inoltre l'SENT booleano, posso aggiungere tali campi e lavorare con loro senza threading preoccupazioni?

Sì, è possibile aggiungere i campi per memorizzare qualche stato. E non è necessario prendere in considerazione la sincronizzazione del filo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top