I just started going trough the Netty 4 examples to see what is new and how to use it in an application. I started with the Discard Server tutorial. I wrote it and run and it worked. I could check via the telnet command. However based upon what is on the netty site I should be able to see something written on the console. But I could not see that though the app is running.
Also I have this warning message : WARNING: Your platform does not provide complete low-level API for accessing direct buffers reliably. Unless explicitly requested, heap buffer will always be preferred to avoid potential risk of getting OutOfMemoryError.
This is the code I have written:
/**
*
*/
package com.smsgh.Discard;
import java.net.InetSocketAddress;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* @author Arsene
*
*/
public class DiscardServer {
/**
*
*/
public DiscardServer() {
}
public static void main(String[] args){
// Instance of the Server bootstrap
ServerBootstrap bootstrap = new ServerBootstrap();
try{
// Bootstrap group is used to handle incoming connections and handle the I/O of
// those connections
bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup());
// Set the factory
bootstrap.channel(NioServerSocketChannel.class);
// Add a child channel handler
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DiscardServerHandler());
}
});
// add the options
bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture future = bootstrap.bind(new InetSocketAddress(8889)).sync();
future.channel().closeFuture().sync();
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
bootstrap.shutdown();
}
}
}
and this is the server handler code:
/**
*
*/
package com.smsgh.Discard;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundByteHandlerAdapter;
/**
* @author Arsene
*
*/
public class DiscardServerHandler extends ChannelInboundByteHandlerAdapter {
/**
*
*/
public DiscardServerHandler() {
}
/*
* (non-Javadoc)
*
* @see
* io.netty.channel.ChannelInboundByteHandlerAdapter#inboundBufferUpdated
* (io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf)
*/
@Override
protected void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in)
throws Exception {
// Here we manipulate the data received
while(in.isReadable()){
System.out.println((char)in.readByte());
System.out.flush();
}
in.clear();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
Can anyone tell me what went wrong? Thanx
I saw the issue. I did not add any encoder nor decoder of message coming in the pipeline.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.