Netty的使用虽然非常灵活,但是基本的步骤很固定,像八股文一样。现总结如下:
1. You create a ServerBootstrap instance to bootstrap the server and bind it later.
2. You create and assign the NioEventLoopGroup instances to handle event processing, such as accepting new connections, receiving data, writing data, and so on.
3. You specify the local InetSocketAddress to which the server binds.
4.You set up a childHandler that executes for every accepted connection.
5. After everything is set up, you call the ServerBootstrap.bind() method to bind the server.
服务端代码示例:
package com.netty.examples.discard;
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;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
public class DiscardServer {
private int port;
public DiscardServer(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(
new DiscardServerHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to
// gracefully
// shut down your server.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 8080;
}
new DiscardServer(port).run();
}
}
相关推荐
netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty开发之nio netty...
springboot+netty开发简易式游戏源码
Netty 是一个基于NIO的客户,服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用。
包括: Netty-4.0开发资源包 Netty-4.1开发资源包 Netty-4.1 API中文开发文档
netty开发包,墙内不太容易下载吧?是做异步通信的,继承了java.nio的功能,更加方便做开发
netty性能实战,初学者必读的netty教程,基于netty4.x
netty中文开发指南,netty是java基于事件的tcp/ip通信技术。
基于 Netty 开发的 Java 游戏服务端框架,目前提供 CocosCreator 和 Unity 的客户端SDK.zip 基于 Netty 开发的 Java 游戏服务端框架,目前提供 CocosCreator 和 Unity 的客户端SDK.zip 基于 Netty 开发的 Java 游戏...
使用netty进行rtsp服务端开发 一个使用netty写的rtsp服务器。 目前支持H264、H265、 AAC格式的流文件上传与存储。 H264、H265、AAC格式流文件的播放
Netty-4.0.23 开发文档(英文原版开发手册)
基于netty 开发 rtmp 推拉流服务器代码,可用ffmpeg代码测试
netty 4.1.0 api chm说明 开发netty必须的说明文档,希望能帮到你
非常详细的Netty视频教程,Netty入门到精通首选
基于netty开发的android端即时通讯,代码能够运行,并有代码注释
Netty入门教程
利用netty开发proxy笔记,和一位大牛netty人员一起讨论出的谈话结果笔记 非常给力
Netty在Android开发中的应用实战系列(一)——— 搭建服务端与客户端:https://azhon.blog.csdn.net/article/details/100569489 Netty在Android开发中的应用实战系列(二)——— Encoder | Decoder | Handler 的...
他人用netty实现的一个聊天demo,包含server,client代码。代码较为简单。
做Java开发,现在很多场合需要分布式应用,很多通信框架的底层实现都包含Netty技术,为了更好了解,值得有兴趣的人研究一下