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

相关推荐
魔力女仆10 小时前
分享一个 JS 鼠标跟随贪吃蛇背景库
开发语言·javascript·计算机外设
2401_8949155311 小时前
GEO 搜索优化完整源码从零部署:环境配置、集群搭建全流程
开发语言·python·tcp/ip·算法·unity
麻瓜老宋11 小时前
AI开发C语言应用按步走,表达式计算器calc的第二十二步,分号赋值链式修复、TOKEN_ASSIGN
c语言·开发语言·atomcode
颜x小13 小时前
[C#] C++与c#语法对比
开发语言·c++·c#
weixin_4236521313 小时前
C#使用JObject
开发语言·c#
国科安芯13 小时前
FreeRTOS RISC-V 浮点上下文切换移植:在 IAR 工程中完整保存 FPU 寄存器
java·开发语言·单片机·嵌入式硬件·算法·系统架构·risc-v
weixin_BYSJ198714 小时前
springboot校园自习室管理小程序---附源码32142
java·javascript·spring boot·python·django·flask·php
霸道流氓气质15 小时前
Java中集成Smile 技术教程:从入门到工程实践
java·开发语言
BerrySen17815 小时前
迈向 Next-Gen Java:企业级高并发架构演进与大模型 Agent 落地深度实战
java·开发语言·架构
大鱼>16 小时前
eBPF内核编程:从TC到XDP的全栈可观测性
linux·服务器·php