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.0modelVersion>
<groupId>com.fly.study.nettygroupId>
<artifactId>nettyartifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
<dependencies>
<dependency>
<groupId>io.nettygroupId>
<artifactId>netty-allartifactId>
<version>4.1.17.Finalversion>
dependency>
dependencies>
project>
主要代码
package demo.back;
import demo.util.PrintHandler;
import demo.util.ServerUtils;
import io.netty.channel.ChannelHandler;
/**
* Created with IntelliJ IDEA
* Description:
* User: weijun.zou
* Date: 2018-06-01
* Time: 17:29
*/
@ChannelHandler.Sharable
public class Server extends PrintHandler {
public static void main(String[] args) throws InterruptedException {
ServerUtils.startServer(9999, new Server());
}
}
package demo.back;
import demo.util.ClientUtils;
import demo.util.PrintHandler;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
/**
* Created with IntelliJ IDEA
* Description:
* User: weijun.zou
* Date: 2018-06-01
* Time: 17:45
*/
public class Client extends PrintHandler {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
final ChannelFuture f = ctx.writeAndFlush("client active");
f.addListener((ChannelFutureListener) future -> {
assert f == future;
ctx.close();
});
}
public static void main(String[] args) throws InterruptedException {
ClientUtils.startClient("localhost", 9999, new Client());
}
}
相关测试代码:
package demo.util;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* Created with IntelliJ IDEA
* Description:
* User: weijun.zou
* Date: 2018-06-01
* Time: 18:14
*/
public class PrintHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(info() + "read");
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(info() + "registered");
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(info() + "unregistered");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(info() + "active");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(info() + "inactive");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println(info() + "readComplete");
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
System.out.println(info() + "triggered");
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
System.out.println(info() + "changed");
}
protected String info() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
return System.currentTimeMillis() + " " + this.getClass().getSimpleName() + ": ";
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
package demo.util;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
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.NioSocketChannel;
/**
* Created with IntelliJ IDEA
* Description:
* User: weijun.zou
* Date: 2018-06-01
* Time: 17:37
*/
public class ClientUtils {
public static void startClient(String host,int port,ChannelHandler... handlers) throws InterruptedException {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap(); // (1)
b.group(workerGroup); // (2)
b.channel(NioSocketChannel.class); // (3)
b.option(ChannelOption.SO_KEEPALIVE, true); // (4)
b.handler(new ChannelInitializer() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(handlers);
}
});
ChannelFuture f = b.connect(host, port).sync(); // (5)
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
}
package demo.util;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
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;
/**
* Created with IntelliJ IDEA
* Description:
* User: weijun.zou
* Date: 2018-06-01
* Time: 17:34
*/
public class ServerUtils {
public static void startServer(int port, ChannelHandler... handlers) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer() { // (4)
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(handlers);
}
})
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
ChannelFuture f = b.bind(port).sync(); // (7)
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}