netty入门文章2

文章目录

1、netty4.x

2、netty实例

正文部分

1、netty4.x

原文:

The minimum requirements to run the examples which are introduced in this chapter are only two;

the latest version of Netty and JDK 1.6 or above.

The latest version of Netty is available in the project download page.

To download the right version of JDK, please refer to your preferred JDK vendor’s web site.

要点:

a.演示例子里边使用的是netty4.x最新版本的架包。

b.JDK1.6或者1.6以上版本。

下载地址:

2、netty实例

2.1、Writing a Discard Server 抛弃协议

原文:

Discard Protocol协议

A useful debugging and measurement tool is a discard service. A discard service simply throws away any data it receives.

要点:

a.接收到什么抛弃什么,它对调试网络状态有一定的用处。

b.服务器就会在TCP端口9检测抛弃协议请求,在建立连接后并检测到请求后,就直接把接收到的数据直接抛弃,直到用户中断连接。

c.UDP协议类似TCP的方式,端口也是9。

代码如下:

DiscardServerHandler 业务处理handler类

代码要点:

a.handler继承ChannelInboundByteHandlerAdapter方式来实现,实在要实现handler接口实现也是OK的。

b.覆写inboundBufferUpdated方法,香港虚拟主机,ByteBuf携带从client发送过来的数据,调用基于抛弃协议实现的clear方法。

c.exceptionCaught方法在出现异常时候执行,虚拟主机,比如可以发送一条错误消息到client端。

DiscardServerHandler

1 package io.netty.example.discard; io.netty.buffer.ByteBuf; 4 import io.netty.channel.ChannelHandlerContext; 5 import io.netty.channel.ChannelInboundByteHandlerAdapter; java.util.logging.Level; 8 import java.util.logging.Logger; * Handles a server-side channel.DiscardServerHandler extends ChannelInboundByteHandlerAdapter {Logger logger = Logger.getLogger(16DiscardServerHandler.class.getName());17 18 @Override19public ByteBuf newInboundBuffer(ChannelHandlerContext ctx) throws Exception {20// Use direct buffer if possible. ctx.alloc().ioBuffer();23 }24 25 @Override inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in)27throws Exception {28while(in.readable()){System.out.flush();31 }in.clear();34 }35 36 @Override exceptionCaught(ChannelHandlerContext ctx,38Throwable cause) throws Exception {logger.log(41 Level.WARNING,42″Unexpected exception from downstream.”,43 cause);44 ctx.close();45 }46 }

代码如下:

DiscardServer 抛弃协议服务器端代码

要点:

netty4.x比起netty3.x做了一些变化

DiscardServer

1 package io.netty.example.discard; io.netty.bootstrap.ServerBootstrap; 4 import io.netty.channel.ChannelFuture; 5 import io.netty.channel.ChannelInitializer; 6 import io.netty.channel.socket.SocketChannel; 7 import io.netty.channel.nio.NioEventLoopGroup; 8 import io.netty.channel.socket.nio.NioServerSocketChannel; * Discards any incoming data. DiscardServer {port; DiscardServer(int port) {18this.port = port;19 }run() throws Exception { {b.group(.channel(NioServerSocketChannel.class)27.childHandler(new ChannelInitializer<SocketChannel>() {28 @OverrideinitChannel(SocketChannel ch) throws Exception {}32 });ChannelFuture f = b.bind(port).sync(); Wait until the server socket is closed.38// In this example, this does not happen, but you can do that to gracefullyf.channel().closeFuture().sync();41} finally {42 b.shutdown();43 }44 }main(String[] args) throws Exception {47int port;48if (args.length > 0) {49port = Integer.parseInt(args[0]);50} else {51port = 8080;52 }53new DiscardServer(port).run();54 }55 }

通过telnet localhost 8080来测试下代码运行性,server端代码稍等改下。

其他client端口见netty包里提供的例子代码。

2.2、Writing an Echo Server 应答协议

原文:

Echo Protocol https://tools.ietf.org/html/rfc862

A very useful debugging and measurement tool is an echo service. An echo service simply sends back to the originating source any data it receives.

要点:

a.类似抛弃协议应答协议也是一个调式的工具服务协议。

b.echo服务协议是将接收到的内容返回给发送方。

c.基于TCP/UDP协议都有实现,同在端口7中监听。

代码如下:

EchoServerHandler 服务器端业务处理handler类

只要有信心,人永远不会挫败

netty入门文章2

相关文章:

你感兴趣的文章:

标签云: