STM32F207以太网+LAN8742传输服务器会卡死
[复制链接]
单片机作为服务器把数据传输到电脑客户端,客户端与服务器是一问一答的通讯方式,传输一段时间后服务器就会卡死,调试后发现卡死后服务器不会进入tcp_echoserver_recv函数
在网上搜了相关问题大多都说是这个接收函数的问题,自己就修改了几行数据接受和发送代码,大神看看问题出现在了那里 |
static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
struct tcp_echoserver_struct *es;
err_t ret_err;
u16 i;
LWIP_ASSERT("arg != NULL",arg != NULL);
es = (struct tcp_echoserver_struct *)arg;
/* if we receive an empty tcp frame from client => close connection */
if (p == NULL)//空帧释放链接
{
/* remote host closed connection */
es->state = ES_CLOSING;
if(es->p == NULL)
{
/* we're done sending, close connection */
tcp_echoserver_connection_close(tpcb, es);
transFlag = 0;
nextFlag = 0;
}
else
{
/* we're not done yet */
/* acknowledge received packet */
tcp_sent(tpcb, tcp_echoserver_sent);
/* send remaining data*/
tcp_echoserver_send(tpcb, es);
}
ret_err = ERR_OK;
}
/* else : a non empty frame was received from client but for some reason err != ERR_OK */
else if(err != ERR_OK)//非空,但是出错,释放缓存
{
/* free received pbuf*/
if (p != NULL)
{
es->p = NULL;
pbuf_free(p);
}
ret_err = err;
}
else if(es->state == ES_ACCEPTED)//连接成功,sent回调
{
/* first data chunk in p->payload */
es->state = ES_RECEIVED;
/* store reference to incoming pbuf (chain) */
es->p = p;
/* initialize LwIP tcp_sent callback function */
tcp_sent(tpcb, tcp_echoserver_sent);
/* send back the received data (echo) */
//20180322
tcp_recved(tpcb, p->tot_len); //yry20180322第一次连接时的数据
memset(&serverRxBuf[serverRxCMDRecePos],0,serverRxCMDCount);
memcpy(&serverRxBuf[serverRxCMDRecePos][0], p->payload, p->len);
serverRxCMDRecePos++;
serverRxCMDRecePos %= serverRxBufSize;
//20190808
tcp_echoserver_pcb = tpcb;
pbuf_free(p);
ret_err = ERR_OK;
}
else if (es->state == ES_RECEIVED)//收到数据
{
/* more data received from client and previous data has been already sent*/
if(es->p == NULL)
{
es->p = p;
/* send back received data */
tcp_recved(tpcb, p->tot_len);
memset(&serverRxBuf[serverRxCMDRecePos],0,serverRxCMDCount);
memcpy(&serverRxBuf[serverRxCMDRecePos][0], p->payload, p->len);
serverRxCMDRecePos++;
serverRxCMDRecePos %= serverRxBufSize;
pbuf_free(p);
//////////////////////
}
else//连接关闭,收到数据
{
struct pbuf *ptr;
/* chain pbufs to the end of what we recv'ed previously */
ptr = es->p;
pbuf_chain(ptr,p);
}
ret_err = ERR_OK;
}
/* data received when connection already closed */
else
{
/* Acknowledge data reception */
tcp_recved(tpcb, p->tot_len);
/* free pbuf and do nothing */
es->p = NULL;
pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;
}
|