[LWIP] LWIP热插拔功能 问题调试

实现了LWIP网络热插拔的功能,但是也出现了一个问题

出现的问题

每次拔掉网线再插上,虽然能重新连接,但是通过调试信息发现每拔插一次网线,上一次的active pcb仍然存在,在拔插3次后,再次连接能够连接成功,但是已经无法通信。通过输出的调试信息查看如下,tcp定时器每次都会处理3个active pcb,而且是最多3个 不会再增加

bash 复制代码
[2025-12-27 19:47:00.604]# RECV ASCII>
tcp_slowtmr: processing active pcb
tcp_slowtmr: polling application
tcp_slowtmr: processing active pcb
tcp_slowtmr: polling application
tcp_slowtmr: processing active pcb
tcp_slowtmr: polling application

原因

检测到网线断开后没有释放资源,但是再次连接网线tcp还能重新连接上。

PCB = Protocol Control Block(协议控制块)

在 LwIP 中,PCB 是网络协议的控制数据结构,用于管理连接状态、缓冲区、定时器等。不同类型的协议有不同的 PCB:TCP PCB:管理 TCP 连接(包含发送/接收窗口、序列号、状态等)

可能的原因1:不是 TCP PCB 数量限制和相关内存池配置

我的工程中关于TCP PCB 数量限制和相关内存池配置 如下

cpp 复制代码
#if !defined MEMP_NUM_TCP_PCB || defined __DOXYGEN__
#define MEMP_NUM_TCP_PCB                5
#endif

#if !defined MEMP_NUM_TCP_PCB_LISTEN || defined __DOXYGEN__
#define MEMP_NUM_TCP_PCB_LISTEN         8
#endif


#if !defined MEMP_NUM_TCP_PCB || defined __DOXYGEN__
#define MEMP_NUM_TCP_PCB                5
#endif

#if !defined MEMP_NUM_TCP_SEG || defined __DOXYGEN__
#define MEMP_NUM_TCP_SEG                16
#endif

配置数量都在3以上,应该是其他原因

可能的原因2:不是 内存空间问题

尝试修改lwip内存空间分配,由3000改为1000

cpp 复制代码
#if !defined MEM_SIZE || defined __DOXYGEN__
#define MEM_SIZE                        3000
#endif

改为

#if !defined MEM_SIZE || defined __DOXYGEN__
#define MEM_SIZE                        1000
#endif

修改后现象仍然是最多3个active pcb,没有变化

原因3 不是

#if !defined IP_REASS_MAXAGE || defined DOXYGEN

#define IP_REASS_MAXAGE 2

#endif

由3改为2,现象没有变化

原因4 不是

#if !defined ARP_QUEUE_LEN || defined DOXYGEN

#define ARP_QUEUE_LEN 2

#endif

由3改为2,现象没有变化

过程为连接网线并开机-》TCP连接读取数据 -》第一次拔插-》TCP连接读取数据-》第二次拔插-》TCP连接读取数据-》第三次拔插-》读取数据失败。

原因5 modbus tcp定义

在lwip之上使用了freemodbus 的modbus-tcp。在文件porttcp.c中有如下定义

cpp 复制代码
#define MB_TCP_CLIENTS  3

将定义数量由3改为2后,最多连接2次,就不能读取数据了。

把数值改为6 #define MB_TCP_CLIENTS 6

在第六次TCP连接时弹出如下调试信息,应该是达到了最多TCP连接限制。

复制代码
[2025-12-27 21:21:19.883]# RECV ASCII>
tcp_slowtmr: processing active pcb
tcp_slowtmr: polling application
tcp_slowtmr: processing active pcb
tcp_slowtmr: polling application
tcp_slowtmr: processing active pcb
tcp_slowtmr: polling application
tcp_slowtmr: processing active pcb
tcp_slowtmr: polling application
tcp_slowtmr: processing active pcb
tcp_slowtmr: polling application
tcpip_thread: PACKET 20008a60
tcpip_thread: PACKET 20008a60

[2025-12-27 21:21:20.085]# RECV ASCII>
tcpip_thread: PACKET 20008a60
tcpip_thread: PACKET 20008a60
TCP connection request 50378 -> 503.
tcpip_thread: PACKET 20008a60
Assertion "tcp_input: pcb->next != pcb (before cache)" failed at line 236 in ..\LwIP\src\core\tcp_in.c
Assertion "tcp_input: pcb->next != pcb (after cache)" failed at line 244 in ..\LwIP\src\core\tcp_in.c
TCP connection established 50378 -> 503.

解决办法

tcp数量不能无限多,那么就需要在断开连接时对资源进行释放。

相关推荐
玄同7658 小时前
从 0 到 1:用 Python 开发 MCP 工具,让 AI 智能体拥有 “超能力”
开发语言·人工智能·python·agent·ai编程·mcp·trae
czy87874758 小时前
深入了解 C++ 中的 `std::bind` 函数
开发语言·c++
JSON_L8 小时前
Fastadmin中使用GatewayClient
php·fastadmin
消失的旧时光-19438 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
yq1982043011568 小时前
静思书屋:基于Java Web技术栈构建高性能图书信息平台实践
java·开发语言·前端
一个public的class8 小时前
你在浏览器输入一个网址,到底发生了什么?
java·开发语言·javascript
Jinkxs8 小时前
Gradle - 与Groovy/Kotlin DSL对比 构建脚本语言选择指南
android·开发语言·kotlin
&有梦想的咸鱼&8 小时前
Kotlin委托机制的底层实现深度解析(74)
android·开发语言·kotlin
青茶3608 小时前
php怎么实现订单接口状态轮询请求
前端·javascript·php
BD_Marathon9 小时前
设计模式——依赖倒转原则
java·开发语言·设计模式