本帖最后由 kael_wyh 于 2020-8-9 02:30 编辑
1、如果开始的时候服务器是开启状态,那么所有状态都正常
2、如果服务器关闭后,lwip连接断开,然后服务器开启,lwip重新创建tcp,会出现连接无反应,状态一直是23、这情况也会出现在从一开始服务器就关闭,然后等重连几次后开启服务器,也会出现连接无反应
4、代码都走到了重连地方
代码:
- cpcb = pCon->cpcb;
- if( isErrorState(cpcb->state) || pCon->lastSendStatus==0 )//
- {
- pCon->isConnected = 0;
-
- if( time - pCon->connectTime > 10000 ) //等10秒
- {
- pCon->connectTime = time;
- pCon->lastSendTime = time;
- pCon->lastSendStatus = 1;
-
- tcp_close(cpcb);
-
- if( isDebug ) printf( "reconnected: %d, %d\r\n", cpcb->state, getActivePcbCount() );
-
- pCon->cpcb = TCP_Client_Init( pCon, getLocalPort(), 0, pCon->ips[0], pCon->ips[1], pCon->ips[2], pCon->ips[3] );
- }
- }
复制代码- struct tcp_pcb * TCP_Client_Init( Con *pCon, u16_t local_port, u32 remove_ip, unsigned char a,unsigned char b,unsigned char c,unsigned char d)
- {
- u8 t;
- struct ip_addr ipaddr;
- struct tcp_pcb *tcp_client_pcb;
-
- err_t err;
-
- if( remove_ip==0 )
- IP4_ADDR(&ipaddr,a,b,c,d); //服务器IP地址
- else ipaddr.addr = remove_ip;
-
- tcp_client_pcb = tcp_new(); /* 建立通信的TCP控制块(Clipcb) */
- if (!tcp_client_pcb)
- {
- trace( "tcp new null\r\n" );
- return NULL;
- }
- pCon->cpcb = tcp_client_pcb;
-
- tcp_client_pcb->flags |= (TF_NODELAY);
- tcp_client_pcb->so_options |= SOF_KEEPALIVE;
- tcp_client_pcb->nrtx = 0;
-
- err = tcp_bind(tcp_client_pcb,IP_ADDR_ANY,local_port); /* 绑定本地IP地址和端口号 ,本地ip地址在LwIP_Init()中已经初始化*/
- if(err != ERR_OK)
- {
- tcp_close(tcp_client_pcb);
- trace( "tcp bind error\r\n" );
- return NULL;
- }
- if( ERR_OK != (t = tcp_connect(tcp_client_pcb,&ipaddr,pCon->port,TCP_Connected)) )
- {
- if( isDebug ) printf( "connect error: %d\r\n", t );
- }
-
- tcp_recv(tcp_client_pcb,TCP_Client_Recv); /* 设置tcp接收回调函数 */
- tcp_err( tcp_client_pcb, tcp_error_callback );
-
- return tcp_client_pcb;
- }
复制代码
|