STM32F4 tcp_client设了两个线程去连接两个服务器,但是10几个小时后两个TCP都断开连接了,也不会在再去重新连接。不知道有没有大神解惑,另外还有一个疑问,tcp_client_thread没有交出CPU主权的函数,他是怎么跳出while(1)循环的。
INT8U tcp_client_init(void)
{
INT8U res;
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL();
res = OSTaskCreate(tcp_client_thread,(void*)0,(OS_STK*)&TCPCLIENT_TASK_STK[TCPCLIENT_STK_SIZE-1],TCPCLIENT_PRIO); //连接服务器1线程
res = OSTaskCreate(tcp_client1_thread,(void*)0,(OS_STK*)&TCPCLIENT1_TASK_STK[TCPCLIENT1_STK_SIZE-1],TCPCLIENT1_PRIO); //连接服务器2线程
OS_EXIT_CRITICAL();
return res;
}
//tcp¿í»§¶ËèÎÎñoˉêy
static void tcp_client_thread(void *arg)
{
OS_CPU_SR cpu_sr;
u32 data_len = 0;
//u8 int_cnt=0;
struct pbuf *q;
err_t err,recv_err;
static ip_addr_t server_ipaddr,loca_ipaddr;
static u16_t server_port,loca_port;
LWIP_UNUSED_ARG(arg);
server_port = REMOTE_PORT;
IP4_ADDR(&server_ipaddr, lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3]);
while (1)
{
tcp_clientconn = netconn_new(NETCONN_TCP); //′′½¨ò»¸öTCPá′½ó
err = netconn_connect(tcp_clientconn,&server_ipaddr,server_port);//ᬽó·tÎñÆ÷
if(err != ERR_OK)
{
netconn_delete(tcp_clientconn); //·μ»ØÖμ2»μèóúERR_OK,é¾3ytcp_clientconnᬽó
printf("client1ᬽóERROR
");
LED2=!LED2;
}
else if (err == ERR_OK) //′|àíDÂᬽóμÄêy¾Y
{
struct netbuf *recvbuf;
tcp_clientconn->recv_timeout = 10;
netconn_getaddr(tcp_clientconn,&loca_ipaddr,&loca_port,1); //»ñ衱¾μØIPÖ÷»úIPμØÖ·oí¶Ë¿úoÅ
printf("client1ᬽóéÏ·tÎñÆ÷%d.%d.%d.%d,±¾»ú¶Ë¿úoÅÎa:%d
",lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3],loca_port);
//IWDG_Init(6,1000);
while(1)
{
if((tcp_client_flag & LWIP_SEND_DATA) == LWIP_SEND_DATA) //óDêy¾Yòa·¢Ëí
{
err = netconn_write(tcp_clientconn ,tcp_client_sendbuf,212,NETCONN_COPY); //·¢Ëítcp_server_sentbufÖDμÄêy¾Y
if(err != ERR_OK)
{
printf("client1·¢Ëí꧰ü
");
}
tcp_client_flag &= ~LWIP_SEND_DATA;
}
if((recv_err = netconn_recv(tcp_clientconn,&recvbuf)) == ERR_OK) //½óêÕμ½êy¾Y
{
OS_ENTER_CRITICAL(); //1ØÖD¶Ï
memset(tcp_client_recvbuf,0,TCP_CLIENT_RX_BUFSIZE); //êy¾Y½óêÕ»o3åÇøÇåáã
for(q=recvbuf->p;q!=NULL;q=q->next) //±éàúíêÕû¸öpbufá′±í
{
//ÅD¶Ïòa¿½±′μ½TCP_CLIENT_RX_BUFSIZEÖDμÄêy¾YêÇ·ñ′óóúTCP_CLIENT_RX_BUFSIZEμÄê£óà¿Õ¼ä£¬èç1û′óóú
//μÄ»°¾íÖ»¿½±′TCP_CLIENT_RX_BUFSIZEÖDê£óà3¤¶èμÄêy¾Y£¬·ñÔòμÄ»°¾í¿½±′ËùóDμÄêy¾Y
if(q->len > (TCP_CLIENT_RX_BUFSIZE-data_len)) memcpy(tcp_client_recvbuf+data_len,q->payload,(TCP_CLIENT_RX_BUFSIZE-data_len));//¿½±′êy¾Y
else memcpy(tcp_client_recvbuf+data_len,q->payload,q->len);
data_len += q->len;
if(data_len > TCP_CLIENT_RX_BUFSIZE) break; //3¬3öTCP¿í»§¶Ë½óêÕêy×é,ìø3ö
}
OS_EXIT_CRITICAL(); //¿aÖD¶Ï
data_len=0; //¸′ÖÆíê3éoódata_lenòaÇåáã¡£
printf("client1 get data : %s
",tcp_client_recvbuf);
if((tcp_client_recvbuf[0]==0x7e)&&(tcp_client_recvbuf[8]==0xe7))
{
if(tcp_client_recvbuf[1]==0xaa)
{
lwipdev.remoteip[0]=tcp_client_recvbuf[2];
lwipdev.remoteip[1]=tcp_client_recvbuf[3];
lwipdev.remoteip[2]=tcp_client_recvbuf[4];
lwipdev.remoteip[3]=tcp_client_recvbuf[5];
REMOTE_PORT= (tcp_client_recvbuf[6]<<8)+tcp_client_recvbuf[7];
datatemp[1]=(tcp_client_recvbuf[2]<<24)+(tcp_client_recvbuf[3]<<16)+(tcp_client_recvbuf[4]<<8)+tcp_client_recvbuf[5];
datatemp[2]=REMOTE_PORT;
STMFLASH_Write(0X08020000,(u32*)datatemp,3);
netconn_close(tcp_clientconn);
netconn_delete(tcp_clientconn);
while(1)
{
delay_ms(500);
delay_ms(500);
printf("system init waitting...
");
}
}
else if(tcp_client_recvbuf[1]==0x55)
{
iwdg_cnt=0;
}
}
netbuf_delete(recvbuf);
tcp_client_recvbuf[0]=0x00;
}else if(recv_err == ERR_CLSD) //1رÕᬽó
{
netconn_close(tcp_clientconn);
netconn_delete(tcp_clientconn);
printf("client1·tÎñÆ÷%d.%d.%d.%d¶Ï¿aᬽó
",lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3]);
break;
}
}
}
}
}
static void tcp_client1_thread(void *arg)
{
OS_CPU_SR cpu_sr;
u32 data_len = 0;
//u8 int_cnt=0;
struct pbuf *q;
err_t err,recv_err;
static ip_addr_t server_ipaddr,loca_ipaddr;
static u16_t server_port,loca_port;
LWIP_UNUSED_ARG(arg);
server_port = REMOTE_PORT;
IP4_ADDR(&server_ipaddr,114,219,135,154);
while (1)
{
tcp_clientconn1 = netconn_new(NETCONN_TCP); //′′½¨ò»¸öTCPá′½ó
err = netconn_connect(tcp_clientconn1,&server_ipaddr,server_port);//ᬽó·tÎñÆ÷
if(err != ERR_OK)
{
netconn_delete(tcp_clientconn1); //·μ»ØÖμ2»μèóúERR_OK,é¾3ytcp_clientconnᬽó
printf("client2ᬽóERROR
");
LED2=!LED2;
}
else if (err == ERR_OK) //′|àíDÂᬽóμÄêy¾Y
{
struct netbuf *recvbuf;
tcp_clientconn1->recv_timeout = 10;
netconn_getaddr(tcp_clientconn1,&loca_ipaddr,&loca_port,1); //»ñ衱¾μØIPÖ÷»úIPμØÖ·oí¶Ë¿úoÅ
printf("client2ᬽóéÏ·tÎñÆ÷%d.%d.%d.%d,±¾»ú¶Ë¿úoÅÎa:%d
",114,219,135,154,loca_port);
while(1)
{/*
if((tcp_client_flag & LWIP_SEND_DATA) == LWIP_SEND_DATA) //óDêy¾Yòa·¢Ëí
{
err = netconn_write(tcp_clientconn1 ,tcp_client_sendbuf,205,NETCONN_COPY); //·¢Ëítcp_server_sentbufÖDμÄêy¾Y
if(err != ERR_OK)
{
printf("client2·¢Ëí꧰ü
");
}
tcp_client_flag &= ~LWIP_SEND_DATA;
}
*/
if((recv_err = netconn_recv(tcp_clientconn1,&recvbuf)) == ERR_OK) //½óêÕμ½êy¾Y
{
OS_ENTER_CRITICAL(); //1ØÖD¶Ï
memset(tcp_client1_recvbuf,0,TCP_CLIENT_RX_BUFSIZE); //êy¾Y½óêÕ»o3åÇøÇåáã
for(q=recvbuf->p;q!=NULL;q=q->next) //±éàúíêÕû¸öpbufá′±í
{
//ÅD¶Ïòa¿½±′μ½TCP_CLIENT_RX_BUFSIZEÖDμÄêy¾YêÇ·ñ′óóúTCP_CLIENT_RX_BUFSIZEμÄê£óà¿Õ¼ä£¬èç1û′óóú
//μÄ»°¾íÖ»¿½±′TCP_CLIENT_RX_BUFSIZEÖDê£óà3¤¶èμÄêy¾Y£¬·ñÔòμÄ»°¾í¿½±′ËùóDμÄêy¾Y
if(q->len > (TCP_CLIENT_RX_BUFSIZE-data_len)) memcpy(tcp_client1_recvbuf+data_len,q->payload,(TCP_CLIENT_RX_BUFSIZE-data_len));//¿½±′êy¾Y
else memcpy(tcp_client1_recvbuf+data_len,q->payload,q->len);
data_len += q->len;
if(data_len > TCP_CLIENT_RX_BUFSIZE) break; //3¬3öTCP¿í»§¶Ë½óêÕêy×é,ìø3ö
}
OS_EXIT_CRITICAL(); //¿aÖD¶Ï
data_len=0; //¸′ÖÆíê3éoódata_lenòaÇåáã¡£
printf("client2 get data : %s
",tcp_client1_recvbuf);
if(tcp_client1_recvbuf[0]==0x7e)
{
if(tcp_client1_recvbuf[1]==0xaa)
{
lwipdev.remoteip[0]=tcp_client1_recvbuf[2];
lwipdev.remoteip[1]=tcp_client1_recvbuf[3];
lwipdev.remoteip[2]=tcp_client1_recvbuf[4];
lwipdev.remoteip[3]=tcp_client1_recvbuf[5];
REMOTE_PORT= (tcp_client1_recvbuf[6]<<8)+tcp_client1_recvbuf[7];
datatemp[1]=(tcp_client1_recvbuf[2]<<24)+(tcp_client1_recvbuf[3]<<16)+(tcp_client1_recvbuf[4]<<8)+tcp_client1_recvbuf[5];
datatemp[2]=REMOTE_PORT;
STMFLASH_Write(0X08020000,(u32*)datatemp,3);
netconn_close(tcp_clientconn);
netconn_delete(tcp_clientconn);
while(1)
{
delay_ms(500);
delay_ms(500);
printf("system init waitting...
");
}
}
else if(tcp_client1_recvbuf[1]==0x55)
{
iwdg_cnt=0;
err = netconn_write(tcp_clientconn1 ,tcp_client1_sendbuf,10,NETCONN_COPY);
if(err != ERR_OK)
{
printf("client2·¢Ëí꧰ü
");
}
}
}
netbuf_delete(recvbuf);
tcp_client1_recvbuf[0]=0x00;
}else if(recv_err == ERR_CLSD) //1رÕᬽó
{
netconn_close(tcp_clientconn1);
netconn_delete(tcp_clientconn1);
printf("client2·tÎñÆ÷%d.%d.%d.%d¶Ï¿aᬽó
",114,219,135,154);
break;
}
}
}
}
}
一周热门 更多>