新加这个函数,并在tcp_in函数里调用一下.出现不通的原因是在网络状态不流畅的情况下,连续连接N次后,LWIP默认不在连接,新建一个变量
//自己做一个函数
struct tcp_pcb *tcp_find_distant(void)
{
#if LWIP_AUTO_FREE_ACTIVE_PCB
struct tcp_pcb *pcb = NULL;
struct tcp_pcb *last_pcb = NULL;
unsigned long last_tick = get_sys_tick();
int pcb_num = 0; //当前一共有几个 配置宏 MEMP_NUM_TCP_PCB
for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
{
pcb_num++;
}
if(pcb_num>=MEMP_NUM_TCP_PCB)
{
for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
{
if(last_tick>pcb->last_tick)
{
last_tick = pcb->last_tick;
last_pcb = pcb;
}
}
}
return last_pcb;
#else
return NULL;
#endif
}
void
tcp_input(struct pbuf *p, struct netif *inp)
{
......
for(lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
if ((ip_addr_isany(&(lpcb->local_ip)) ||
ip_addr_cmp(&(lpcb->local_ip), &(iphdr->dest))) &&
lpcb->local_port == tcphdr->dest) {
/* Move this PCB to the front of the list so that subsequent
lookups will be faster (we exploit locality in TCP segment
arrivals). */
if (prev != NULL) {
((struct tcp_pcb_listen *)prev)->next = lpcb->next;
/* our successor is the remainder of the listening list */
lpcb->next = tcp_listen_pcbs.listen_pcbs;
/* put this listening pcb at the head of the listening list */
tcp_listen_pcbs.listen_pcbs = lpcb;
}
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for LISTENing connection.
"));
//新加的代码
//无法申请到内存块,说明当前已连接的PCB已达到上限,释放最近的active中的PCB块
if(tcp_listen_input(lpcb)==ERR_MEM)
{
struct tcp_pcb *destroy_pcb = tcp_find_distant();
if(destroy_pcb!=NULL)
{
tcp_abandon(destroy_pcb, 1);
//tcp_close(destroy_pcb);
goto LBL_RESTART;
}
}
pbuf_free(p);
return;
}
prev = (struct tcp_pcb *)lpcb;
}
}