編寫一個discard服務器程序
<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="font-family:Microsoft YaHei;">import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandlerAdapter;
/**
</span></span>
<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="font-family:Microsoft YaHei;"> * 處理一個服務器通道
*/
public class DiscardServerHandler extends ChannelHandlerAdapter { // (1)
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
// 直接調用release來釋放接收到的數據
((ByteBuf) msg).release(); // (3)
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
// 當異常發生時,關閉處理器
cause.printStackTrace();
ctx.close();
}
}</span></span>
1.DiscardServerHandler繼承了ChannelHandlerAdapter類,這個類是ChannelHandler的一個實現類。其中ChannelHandler提供了各類事件的處理器方法方便我們來覆蓋。但是,現在繼承ChannelHandlerAdapter類比實現ChannelHandler更方便。<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="font-family:Microsoft YaHei;">@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
try {
// Do something with msg
} finally {
ReferenceCountUtil.release(msg);
}
}
</span></span>
4.exceptionCaught()事件處理器方法主要適用於一個異常發生的時候,這個異常可能是在netty中被一個io錯誤引起或者一個<span style="font-family:Microsoft YaHei;font-size:14px;">package com.zzj.nio.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* 拋棄消息服務端
* @author zzj
* @date Oct 19, 2016 1:24:05 PM
*/
public class DiscardServer {
/**端口號**/
private int port;
/**
* 構造函數
* @param port
*/
public DiscardServer(int port){
this.port = port;
}
public static void main(String[] args) {
int port = 9999;
try {
//開啓服務端接受消息
new DiscardServer(port).run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 開啓服務器
* @author zzj
* @throws InterruptedException
*/
public void run() throws InterruptedException{
EventLoopGroup bossGroup = new NioEventLoopGroup();//(1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();//(2)
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)//(3)
.childHandler(new ChannelInitializer<SocketChannel>() {//(4)
@Override
protected void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(new DiscardServerHandler());
}
});
bootstrap.option(ChannelOption.SO_BACKLOG, 128)//(5)
.childOption(ChannelOption.SO_KEEPALIVE, true);//(6)
//緊接着,綁定並且開始接受請求的連接
ChannelFuture future = bootstrap.bind(port).sync();
//等待直到服務器socket被關閉,在這裏例子中,這個不會發生,但是你可以這樣寫
future.channel().closeFuture().sync();
}finally{
//優雅的關閉
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
</span>
1.NioEventLoopGroup是一個用戶處理io操作的多線程event loop.Netty對於不同種類的傳輸提供了多種EventLoopGroup實現方式。在這個例也許你想通過增加一些處理類比如DiscardServerHandle來配置一個新的Channel或者其對應的ChannelPipeline來實現你的網絡程序。隨着應用程序
如果,想要在服務器中打印出客戶端輸入的消息,那麼可以使用一下代碼:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
try {
while (in.isReadable()) { // (1)
System.out.print((char) in.readByte());
System.out.flush();
}
} finally {
ReferenceCountUtil.release(msg); // (2)
}
}