本系列教程包括:
IOT云平台 simple(0)IOT云平台简介
IOT云平台 simple(1)netty入门
IOT云平台 simple(2)springboot入门
IOT云平台 simple(3)springboot netty实现TCP Server
IOT云平台 simple(4)springboot netty实现简单的mqtt broker
IOT云平台 simple(5)springboot netty实现modbus TCP Master
IOT云平台 simple(6)springboot netty实现IOT云平台基本的架构(mqtt、Rabbitmq)
netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。
入门工程中分别创建了server、client,建立连接后client向server发送字符串“hello world!”。
第1步:创建server端:主要类关系图如下:
ServerStarter:启动server的类
TestServer:server类
TestServerChannelInitializer:server channel初始化的类
TestServerHandler:server channel处理的类
1)ServerStarter类的代码:
public class ServerStarter {
public static void main(String[] args) throws Exception {
System.out.println("server start...");
int port = 8081;
new TestServer(port).start();
}
}
2)TestServer类的代码:
public class TestServer {
private int port;
private EventLoopGroup bossGroup;// 多线程事件循环器:接收的连接
private EventLoopGroup workGroup; // 多线程事件循环器:处理已经被接收的连接
private ChannelFuture channelFuture;
public TestServer(int port) {
this.port = port;
}
public void start() {
bossGroup = new NioEventLoopGroup();
workGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new TestServerChannelInitializer())
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
// 绑定端口,开始接收进来的连接
channelFuture = serverBootstrap.bind(port).sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
destroy();
}
}
public void destroy() {
// 关闭socket
try {
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
bossGroup = null;
workGroup = null;
}
}
3)TestServerChannelInitializer类的代码:
public class TestServerChannelInitializer extends ChannelInitializer {
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(new TestServerHandler());
}
}
4)TestServerHandler类的代码:
public class TestServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf byteBuf = (ByteBuf) msg;
try {
System.out.println("客户端:" + ctx.channel().remoteAddress());
System.out.println("收到:" + byteBuf.toString(CharsetUtil.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
// 当出现异常就关闭连接
cause.printStackTrace();
ctx.close();
}
}
第2步:然后创建client端:
主要类关系图如下:
1)ClientStarter类的代码如下:
public class ClientStarter {
public static void main(String[] args) throws Exception {
System.out.println("client start...");
String ip = "127.0.0.1";
int port = 8081;
new TestClient(ip, port).start();
}
}
2)TestClient类的代码如下:
public class TestClient {
private String serverIp = "";//服务器的IP
private int serverPort = 8080;//服务器的端口
public TestClient(String serverIp, int serverPort) {
this.serverIp = serverIp;
this.serverPort = serverPort;
}
public void start() throws Exception {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap clientBootstrap = new Bootstrap();
clientBootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.handler(new TestClientChannelInitializer());
//连接服务器
ChannelFuture channelFuture = clientBootstrap.connect(serverIp, serverPort).sync();
//对通道关闭进行监听
channelFuture.channel().closeFuture().sync();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
}
3)TestClientChannelInitializer类的代码如下:
public class TestClientChannelInitializer extends ChannelInitializer {
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(new TestClientHandler());
}
}
4)TestClientHandler类的代码如下:
public class TestClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) {
//发送消息到服务端
ctx.writeAndFlush(Unpooled.copiedBuffer("hello world!", CharsetUtil.UTF_8));
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf byteBuf = (ByteBuf) msg;
System.out.println("服务端:" + ctx.channel().remoteAddress());
System.out.println("消息:" + byteBuf.toString(CharsetUtil.UTF_8));
}
}
第3步:运行ServerStarter启动server端:
第4步:运行ClientStarter启动clientr端:
查看运行结果,server端已经收到了字符串“hello world!”。
代码详见:
https://gitee.com/linghufeixia/iot-simple
code1