|
看了好几个TCP客户端的例程(硬汉哥在论坛分享的TCP Client例程、正点原子的例程),发现大家在接收回调函数的时候,都会有一个判断,当接收到服务器的数据包为空的时候,就关闭连接,为什么要这么做呢,不是很理解。我感觉应该不用的呀
- static err_t tcp_echoclient_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
- {
- struct echoclient *es;
- err_t ret_err;
- LWIP_ASSERT("arg != NULL",arg != NULL);
- es = (struct echoclient *)arg;
- /* 如果我们从TCP服务器接收到空帧 => 关闭连接 */
- if (p == NULL)
- {
- /* 远程主机关闭连接 */
- es->state = ES_CLOSING;
- if(es->p_tx == NULL)
- {
- /* 发送完成,关闭连接 */
- tcp_echoclient_connection_close(tpcb, es);
- }
- else
- {
- /* 继续发送数据 */
- tcp_echoclient_send(tpcb, es);
- }
-
- ret_err = ERR_OK;
- }
- /* 否则 : 接收到非空帧,但是err != ERR_OK */
- else if(err != ERR_OK)
- {
- /* 是否接收pbuf */
- if (p != NULL)
- {
- pbuf_free(p);
- }
- ret_err = err;
- }
- else if(es->state == ES_CONNECTED)
- {
- /* 加一接收计数 */
- message_count++;
-
- /* 确认数据接收 */
- tcp_recved(tpcb, p->tot_len);
- pbuf_free(p);
- tcp_echoclient_connection_close(tpcb, es);
- ret_err = ERR_OK;
- }
- /* 当连接已经关闭后数据接收 */
- else
- {
- /* 确认数据接收 */
- tcp_recved(tpcb, p->tot_len);
- /* 释放 pbuf */
- pbuf_free(p);
- ret_err = ERR_OK;
- }
-
- return ret_err;
- }
复制代码
|
|