我刚刚开始通过 Netty 4 示例来了解新功能以及如何在应用程序中使用它。我从 Discard Server 教程开始。我编写并运行它并且它有效。我可以通过 telnet 命令进行检查。然而,根据 netty 网站上的内容,我应该能够看到控制台上写的东西。但是,尽管应用程序正在运行,但我看不到这一点。
我也有这个警告消息:警告:您的平台没有提供完整的低级 API 来可靠地访问直接缓冲区。除非明确要求,否则始终首选堆缓冲区以避免出现 OutOfMemoryError 的潜在风险。
这是我写的代码:
/**
*
*/
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();
}
}
}
这是服务器处理程序代码:
/**
*
*/
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();
}
}
谁能告诉我出了什么问题?谢谢