新手上路
- 积分
- 28
- 金钱
- 28
- 注册时间
- 2018-8-23
- 在线时间
- 5 小时
|

楼主 |
发表于 2018-8-28 13:54:07
|
显示全部楼层
问题已解决!
之前用的是TCP_DEBUG,后来我又打开HTTPD_DEBUG,发现输出了一条信息:http_poll: too many retries, close 输出这条信息后,才开始关闭tcp连接的。
查找源代码发现,这是在http_poll函数中发生的,这个函数,每2秒调用一次,8秒内如果没有数据传输的话,就关闭。其中关键参数是 hs->retries!这个参数累加到4,就会执行关闭连接。
参考其他的官方代码,发现每次有数据传输,send或recv,都会重置retries为0;查看自己写的代码,虽然每帧数据都有接收并处理,但是没有重置retries,导致系统认为是空闲链接,从而超时后自动关闭!
[mw_shl_code=c,true]/**
* The poll function is called every 2nd second.
* If there has been no data sent (which resets the retries) in 8 seconds, close.
* If the last portion of a file has not been sent in 2 seconds, close.
*
* This could be increased, but we don't want to waste resources for bad connections.
*/
static err_t http_poll(void *arg, struct tcp_pcb *pcb)
{
struct http_state *hs = (struct http_state *)arg;
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: pcb=%p hs=%p pcb_state=%s\n",
(void*)pcb, (void*)hs, tcp_debug_state_str(pcb->state)));
if (hs == NULL) {
/* arg is null, close. */
LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: arg is NULL, close\n"));
http_close_conn(pcb, hs);
return ERR_OK;
} else {
hs->retries++;
if (hs->retries == HTTPD_MAX_RETRIES) {
LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: too many retries, close\n"));
http_close_conn(pcb, hs);
return ERR_OK;
}
/* If this connection has a file open, try to send some more data. If
* it has not yet received a GET request, don't do this since it will
* cause the connection to close immediately. */
if(hs && (hs->handle)) {
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: try to send more data\n"));
if(http_send_data(pcb, hs)) {
/* If we wrote anything to be sent, go ahead and send it now. */
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("tcp_output\n"));
tcp_output(pcb);
}
}
}
return ERR_OK;
}[/mw_shl_code]
|
|