1、学习netty第一天,示例程序
服务端代码
package com.guoyy.server;
import com.guoyy.handler.DiscardServerHandler;
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.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* @author guoyy
* 接收客户端的数据,不做处理
*/
public class DiscardServer {
public static void main(String[] args) {
DiscardServer ds = new DiscardServer();
try {
ds.run(8080);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(int port) throws Exception{
// 主事件循环组(用来接收客户的连接)
EventLoopGroup bossGroup = new NioEventLoopGroup();
// 工作事件循环组(用来接收主事件循环组接收的连接并将连接注册到工作事件循环组)
EventLoopGroup workGroup = new NioEventLoopGroup();
// 帮助启动服务的类
ServerBootstrap server = new ServerBootstrap();
try {
// 设置主线程池和工作线程池
server.group(bossGroup, workGroup)
// 当接收到一个新的连接时实例化一个通道的时候创建的通道类型
.channel(NioServerSocketChannel.class)
// 当新接收的channel用于设置用户来配置该channel
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
// DiscardServerHandler是用来接收和丢弃数据的处理类
// 获取channel的通道链,并在最后添加DiscardServerHandler
ch.pipeline().addLast(new DiscardServerHandler());
}
})
// 设置等待连接数(主要是在三次握手中还没有被accept的连接数)
.option(ChannelOption.SO_BACKLOG, 128)
// 保持连接
.childOption(ChannelOption.SO_KEEPALIVE, true);
// 绑定端口和启动服务
ChannelFuture future = server.bind(port).sync();
System.out.println("server start on :" + port);
// 等待服务关闭(在服务端不会发生)
future.channel().closeFuture().sync();
} catch (Exception e) {
throw e;
} finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}
package com.guoyy.handler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class DiscardServerHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
((ByteBuf) msg).release();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
客户端用telnet来验证
pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guoyy</groupId>
<artifactId>nettydemo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>nettyserver</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
<version>4.1.27.Final</version>
</dependency>
<!-- <dependency>
<groupId>io.netty</groupId>
<artifactId>netty-example</artifactId>
<version>4.1.27.Final</version>
</dependency> -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>