SpringBoot中使用Netty实现TCP通讯,服务器主动向客户端发送数据
简述:
Springboot项目的web服务后台,web服务运行在9100端口。
后台使用netty实现了TCP服务,运行在8000端口。
启动截图如下:
pom依赖
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.25.Final</version>
</dependency>
netty服务代码
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
/**
* netty服务器,主要用于与客户端通讯
*/
public class NettyServer {
private final int port; //监听端口
public NettyServer(int port) {
this.port = port;
}
//编写run方法,处理客户端的请求
public void run() throws Exception {
//创建两个线程组
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(); //8个NioEventLoop
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//获取到pipeline
ChannelPipeline pipeline = ch.pipeline();
//向pipeline加入解码器
pipeline.addLast("decoder", new StringDecoder());
//向pipeline加入编码器
pipeline.addLast("encoder", new StringEncoder());
//加入自己的业务处理handler
pipeline.addLast(new NettyServerHandler());
}
});
System.out.println("netty服务器启动成功(port:" + port + ")......");
ChannelFuture channelFuture = b.bind(port).sync();
//监听关闭
channelFuture.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
netty业务处理handler
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class NettyServerHandler extends SimpleChannelInboundHandler<String> {
//定义一个channle 组,管理所有的channel
//GlobalEventExecutor.INSTANCE) 是全局的事件执行器,是一个单例
public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
/**
* 有客户端与服务器发生连接时执行此方法
* 1.打印提示信息
* 2.将客户端保存到 channelGroup 中
*/
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
System.err.println("有新的客户端与服务器发生连接。客户端地址:" + channel.remoteAddress());
channelGroup.add(channel);
}
/**
* 当有客户端与服务器断开连接时执行此方法,此时会自动将此客户端从 channelGroup 中移除
* 1.打印提示信息
*/
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
System.err.println("有客户端与服务器断开连接。客户端地址:" + channel.remoteAddress());
}
/**
* 表示channel 处于活动状态
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().remoteAddress() + " 处于活动状态");
}
/**
* 表示channel 处于不活动状态
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().remoteAddress() + " 处于不活动状态");
}
/**
* 读取到客户端发来的数据数据
*/
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
//获取到当前channel
Channel channel = ctx.channel();
System.err.println("有客户端发来的数据。地址:" + channel.remoteAddress() + " 内容:" + msg);
}
/**
* 处理异常
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("发生异常。异常信息:{}", cause.getMessage());
//关闭通道
ctx.close();
}
}
Springboot项目中启动netty服务
启动类修改:

测试结果截图
服务启动,及客户端连接

客户端给服务器发送数据

服务器给客户端发送数据
服务器查看当前所有连接的客户端

服务器获取到所有客户单的ip地址及端口号后,即可通过其给指定客户端发送数据


SpringBoot中使用Netty实现TCP通讯,服务器主动向客户端发送数据的更多相关文章
- SSH服务器与Android通信(3)--Android客户端发送数据
Android客户端向SSH服务器发送数据主要有三种情况:通过客户端删除数据.添加数据和修改数据. 1.删除数据 先看看jsp文件里面是怎样删除数据的: <td align="cent ...
- 【Java】学习路径60-利用TCP协议接收多个客户端的数据
import java.io.IOException; import java.net.*; public class TCP_Server { public static void main(Str ...
- 2.jdk1.8+springboot中http1.1之tcp连接复用实现
接上篇:https://www.cnblogs.com/Hleaves/p/11284316.html 环境:jdk1.8 + springboot 2.1.1.RELEASE + feign-hys ...
- android socket 通讯(客户端) 发送数据
/** ClientSocket通讯类 **/ public class ClientSocket { /**服务器地址*/ private String serverUrl=&q ...
- java后台服务器向Nodejs客户端发送压缩包文件
java代码: Map map=new HashMap(); try { //获取本地文件转换成字符换 File file = new File(apppath);//"D:/upload/ ...
- vue中axios的post请求使用form表单格式发送数据
vue使用插件qs实现 (qs 是一个增加了一些安全性的查询字符串解析和序列化字符串的库.) 在jquery中的ajax的方法已将此封装,所以不需要再次序列化 1. 安装 在项目中使用命令行工具输 ...
- TCP实现多个客户端发送数据给服务器端
SocketThread给服务端用的线程类: public class SocketThread extends Thread{ private Socket socket; public Socke ...
- Dynemic Web Project中使用servlet的 doGet()方法接收来自浏览器客户端发送的add学生信息形成json字符串输出到浏览器并保存到本地磁盘文件
package com.swift.servlet; import java.io.FileOutputStream;import java.io.IOException;import java.io ...
- C#中的TCP通讯与UDP通讯
最近做了一个项目,主要是给Unity3D和实时数据库做通讯接口.虽然方案一直在变:从开始的UDP通讯变为TCP通讯,然后再变化为UDP通讯;然后通讯的对象又发生改变,由与数据库的驱动进行通讯(主动推送 ...
- 基于STM32和W5500的Modbus TCP通讯
在最近的一个项目中需要实现Modbus TCP通讯,而选用的硬件平台则是STM32F103和W5500,软件平台则选用IAR EWAR6.4来实现. 1.移植千的准备工作 为了实现Modbus TCP ...
随机推荐
- 阿里巴巴LangEngine开源了!支撑亿级网关规模的高可用Java原生AI应用开发框架
LangEngine作为阿里集团内部发起的纯Java版本的AI应用开发框架,经过充分实践,已经广泛应用于包括淘宝.天猫.阿里云.爱橙科技.菜鸟.蚂蚁.飞猪.1688.LAZADA等在内的多个业务场景. ...
- window.open打开网址被拦截
window.open打开网址被拦截 标签: js 坑位 通过window.open打开一个网址,在火狐和IE系列浏览器下会拦截掉,除非用户主动点击允许才会成功,这样用户体验基本是恶心到产品的,而产品 ...
- Docker跨主机跨服务器迁移
主要作用: 就是让配置好的容器,可以得到复用,后面用到得的时候就不需要重新配置. 其中涉及到的命令有: docker commit 将容器保存为镜像 docker save -o 将镜像备份为tar ...
- 10C++选择结构(4)——教学
一.switch语句 (第25课 成绩等级) 问题:风之巅小学规定,若测试成绩大于或等于90分为"A",大于或等于70分小于90分为"B",大于或等于60分小于 ...
- 使用 Azure AI Studio 构建和部署使用提示流的问答助驾系统
使用 Azure AI Studio 构建和部署使用提示流的问答助驾系统 See: Build and deploy a question and answer copilot with prompt ...
- T语言开发笔记1
为什么会有开发语言的想法 在2012年,我准备开发一个给前端切图使用的屏幕取色器. 需求很简单,前端经常需要获取设计稿特定位置的颜色代码.虽然当时 PhotoShop 提供了内部取色器,但操作麻烦,而 ...
- iOS app 自动化测试 - 环境搭建
1. 基本前提 安装好了 mac 上自动化测试的基本环境 如果没有,可以参考这一个: 2. iOS appium python自动化测试环境搭建 2.1 真机环境 2.1.1 前提:安装了 appiu ...
- docker limit
我给docker的systemd
- Spring boot 2.0 之优雅停机
spring boot 框架在生产环境使用的有一段时间了,它"约定大于配置"的特性,体现了优雅流畅的开发过程,它的部署启动方式(java -jar xxx.jar)也很优雅.但是我 ...
- error C2664: “HANDLE FindFirstFileW(LPCWSTR,LPWIN32_FIND_DATAW)”: 无法将参数 1 从“const _Elem *”转换为“LPCWSTR”
Error 30 error C2664: 'HANDLE FindFirstFileW(LPCWSTR,LPWIN32_FIND_DATAW)' : 不能将参数 1 从"char [260 ...