Netty4.x 用户指南 -入门

Getting Started

This chapter tours around the core constructs of Netty with simple examples to let you get started quickly. You will be able to write a client and a server on top of Netty right away when you are at the end of this chapter.

If you prefer top-down approach in learning something, you might want to start from Chapter 2, Architectural Overview and get back here.

入门

本章将以简单的例子来介绍Netty的核心结构,以便您快速入门。在本章结束时,您能够立即在Netty之上编写客户端和服务端。

如果您喜欢自顶向下的学习方法,您可能希望从第2章体系结构概述开始,然后回到这里。

Before Getting Started

The minimum requirements to run the examples 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.

As you read, you might have more questions about the classes introduced in this chapter. Please refer to the API reference whenever you want to know more about them. All class names in this document are linked to the online API reference for your convenience. Also, please don’t hesitate to contact the Netty project community and let us know if there’s any incorrect information, errors in grammar and typo, and if you have a good idea to improve the documentation.

开始之前

运行本章中示例的最低要求只有两个; 最新版本的Netty和JDK 1.6或更高版本。 最新版本的Netty可以在项目下载页面中找到。 要下载正确版本的JDK,请参阅您首选的JDK供应商的网站。

在您阅读时,您可能会对本章中介绍的类有更多疑问。 每当你想了解更多关于它们的信息时,请参考API参考。 为了您的方便,本文档中的所有类名都链接到在线API参考。 另外,请不要犹豫联系Netty项目社区,并告诉我们是否有任何不正确的信息,语法错误和错字,以及是否有改进文档的好想法。

Writing a Discard Server

The most simplistic protocol in the world is not ‘Hello, World!’ but DISCARD. It’s a protocol that discards any received data without any response.

To implement the DISCARD protocol, the only thing you need to do is to ignore all received data. Let us start straight from the handler implementation, which handles I/O events generated by Netty.

编写一个Discard Server

世界上最简单的协议不是’Hello, World!’ ,而是DISCARD。它是一个丢弃任何接收到的数据没有任何响应的协议。

要实现DISCARD协议,您只需要忽略所有接收到的数据。让我们直接从处理器实现开始,它处理由Netty产生的I/O事件。

package io.netty.example.discard;

import io.netty.buffer.ByteBuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Handles a server-side channel.
 */
public class DiscardServerHandler extends ChannelInboundHandlerAdapter { // (1)

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
        // Discard the received data silently.
        ((ByteBuf) msg).release(); // (3)
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
}
  1. DiscardServerHandler 继承ChannelInboundHandlerAdapter,它是ChannelInboundHandler的实现。ChannelInboundHandler 提供了可重写的各种事件处理方法。目前,只需继承 ChannelInboundHandlerAdapter 而不是自己实现处理器接口。
  2. 我们重写了channelRead() 事件处理方法,每当接收到来自客户端的新数据时,这个方法都会被调用接收数据。在这个例子中,接收到的消息类型是ByteBuf
  3. 为了实现DISCARD 协议,处理器必须忽略接收到的消息,ByteBuf 是一个引用计数对象,必须通过release() 方法明确释放。请记住,处理器的职责是释放传递给处理器的任何引用计数对象。通常,channelRead() 处理器方法的实现如下所示:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    try {
        // Do something with msg
    } finally {
        ReferenceCountUtil.release(msg);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值