[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数量不能无限多,那么就需要在断开连接时对资源进行释放。

相关推荐
crossaspeed17 小时前
Java-List,Set,Map(八股)
java·开发语言·list
LawrenceLan17 小时前
Flutter 零基础入门(十八):StatefulWidget 生命周期初识
开发语言·前端·flutter·dart
代码or搬砖17 小时前
java并发编程
java·开发语言
小李独爱秋17 小时前
计算机网络经典问题透视:IP电话的通话质量与哪些因素有关?
服务器·开发语言·网络·网络协议·tcp/ip·计算机网络
BHXDML18 小时前
第九章: STL 容器基础(C++)
开发语言·c++
董世昌4118 小时前
什么是暂时性死区?
开发语言·前端·javascript
茉莉玫瑰花茶18 小时前
C++11 扩展 - 模板元编程
开发语言·c++
六毛的毛18 小时前
hot100 python解法合集
开发语言·python
学嵌入式的小杨同学18 小时前
【嵌入式 C 语言实战】手动实现字符串四大核心函数(strcpy/strcat/strlen/strcmp)
c语言·开发语言·前端·javascript·数据结构·数据库·算法
拾贰_C18 小时前
[python | numpy] numpy& matplotib冲突
开发语言·python·numpy