【转】Lwip 断连,连接几次后不通及偶尔不通的问题.

版权声明:本文为博主unsv29原创文章,未经博主允许不得转载。 https://blog.csdn.net/unsv29/article/details/82562479

https://blog.csdn.net/hecong_kit/article/details/24415693

新加这个函数,并在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.\n"));

        //新加的代码
        //无法申请到内存块,说明当前已连接的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;
    }
  }

LwIP BUG之TCP连接丢失

注:本文转自老衲五木的博客! LwIP所有版本包括最新的2.0版本具有以下缺陷,当用户使用raw编程并在err或poll回调函数中操作了内核全局tcp_active_pcbs链表(最典型的,比如进行了...

  • lj570681983

    尒槑: 太感谢了,成功的解决了我的问题,困扰我好久了(05-24 00:06#3楼)

  • hecong129

    辧聪: 时间太久远了,很久不做这块,应该是进到连接网络的位置(11-08 11:13#2楼)

猜你喜欢

转载自blog.csdn.net/unsv29/article/details/82562479
今日推荐