I have some reason that do not want to directly use netty's client, as Java
has SocketChannel, I have a write() method to write the object class to
bytes via serialization then send to sever, but netty is bytebuf receiving:
The SocketChannel.write() is:
public void write(Object object) {
try {
System.out.println("The client writting at " +
LocalDateTime.now() + " to " + this.host + ":" + this.port + " for " +
this.name);
this.sc.write(ByteBuffer.wrap(convertObjectToByte(object)));
System.out.println("The client writed at " +
LocalDateTime.now() + " to " + this.host + ":" + this.port + " for " +
this.name);
} catch (IOException ex) {
System.out.println("The client failed to write at " +
LocalDateTime.now() + " to " + this.host + ":" + this.port + " for " +
this.name);
this.connect();
Logger.getLogger(Task.class.getName()).log(Level.SEVERE,
null, ex);
}
}
The Netty server is:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
System.out.println(msg.getClass()); <-- I printed out the
class is UnpoolUnsafeDirectBuffer.....
if (msg instanceof UptimeHttp) {
UptimeHttp uptimeHttp = (UptimeHttp) this.object;
System.out.println("The server processed at " +
LocalDateTime.now() + " from " +
ctx.channel().remoteAddress().toString().replace("/", "") + " for " +
uptimeHttp.HttpRequestAddress);
} else {
System.out.println("An unknown object");
}
}
So how can I convert to just using convertByteToObject method in
channelRead?
public static final Object convertByteToObject(byte[] bytes) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = new ObjectInputStream(bis);
return in.readObject();
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(EchoServerHandler.class.getName()).log(Level.SEVERE, null,
ex);
return null;
}
}
Thanks!
--
You received this message because you are subscribed to the Google Groups
"Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/netty/5df6aeaf-eaf3-4106-92d2-03576c9211c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.