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

相关推荐
千里码aicood13 分钟前
基于Python的电商数据采集系统(大数据+爬虫)
开发语言·python
陈皮波比茶39 分钟前
Python项目实战-网络机器人(爬虫)
开发语言·网络·爬虫·python·机器人
法哥20122 小时前
用 C++ 控制 CMW100,到底在干什么?
开发语言·c++
QH139292318802 小时前
NVIDIA H200 H300服务器
运维·服务器·开发语言·网络·信息与通信
xixiaoyunya2 小时前
中小微企业轻量组网方案的技术选型与实践:从传统专线到软定义隧道的成本与效能分析
服务器·php·apache
OPEN-F2 小时前
Python进阶教程:单元测试与代码质量
开发语言·python·单元测试
阿里嘎多学长2 小时前
2026-08-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
其实防守也摸鱼2 小时前
Codex破局:前端组件秒级生成的技术文章大纲
开发语言·前端·人工智能·学习·安全·web安全
Logintern092 小时前
装饰器和洋葱模式的区分
开发语言·python·架构
知行产研3 小时前
中国矿山无人驾驶出海的三重门
数据库·mysql·php