tutorial tuning examples java sockets jboss network-programming netty

java - examples - netty performance tuning



Netty channel.write no escribe el mensaje (1)

Verá por qué falla si reemplaza su

ctx.write("Ack"); ctx.flush();

en su serverHandler con lo siguiente:

ChannelFuture cf = ctx.write("Ack"); ctx.flush(); if (!cf.isSuccess()) { System.out.println("Send failed: " + cf.cause()); }

Debería darle un mensaje diciendo que String no es compatible.

ByteBuf debería funcionar:

ctx.write(Unpooled.copiedBuffer("Ack", CharsetUtil.UTF_8)); ctx.flush();

en el lado del cliente edita el método channelRead:

ByteBuf in = (ByteBuf) msg; System.out.println("Client received: " + in.toString(CharsetUtil.UTF_8));

Estoy tratando de dar mis primeros pasos con Netty, para este propósito escribí un servidor simple en Netty y un cliente simple en oio plain TCP.

El cliente envía un paquete de texto aleatorio y debe recibir el mensaje "ack". Ver el método del controlador:

@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ctx.write("Ack"); ctx.flush(); ByteBuf in = (ByteBuf) msg; StringBuilder sb = new StringBuilder(); try { while (in.isReadable()) { sb.append((char) in.readByte()); } } finally { ReferenceCountUtil.release(msg); } LOG.debug("Incoming message. ACK was send"); String myaddr = ctx.channel().remoteAddress().toString(); String message = "Message from " + myaddr + " :" + sb.toString(); LOG.debug(message); sendToOther(myaddr, message); }

El problema es que cuando trato de enviar una cadena de respuesta "Ack", el cliente no recibe nada. Pero cuando trato de enviar un mensaje de respuesta entrante, funciona bien y veo eco en mi cliente.

@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ctx.write(msg); ctx.flush();

El método write () necesita Object e intenté enviar (Object) String, pero no pasó nada. También traté de enviar ByteBuf (lo vi en un artículo) y todavía no funciona.

Cuando envío un mensaje entrante como eco, funciona. Cuando envío algo más, no es así. Por favor, ayúdenme, simplemente no puedo entender dónde está mi error.

Resolví este problema. El punto era que solo debes enviar ByteBuff. Entonces, necesitamos crearlo, y escribirle algo y solo entonces podemos escribirlo en Chanel Pipe. en mi caso fue como:

String ack = "ACK"; ByteBuf out = ctx.alloc().buffer(ack.length()*2); out.writeBytes(ack.getBytes()); ctx.write(out); ctx.flush(); LOG.debug("Incoming message. ACK was send");

Puede ser que esta no sea una solución excelente, pero funciona como ejemplo.