[mw_shl_code=c,true]
[mw_shl_code=c,true]static err_t TcpServer_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
if (p != NULL)
{
tcp_write(pcb,p,p->tot_len,1);
}
else if (err == ERR_OK)
{
return tcp_close(pcb);
}
pbuf_free(p);
return ERR_OK;
}
/**
* @brief This function when the Telnet connection is established
* @param arg user supplied argument
* @param pcb the tcp_pcb which accepted the connection
* @param err error value
* @retval ERR_OK
*/
static err_t TcpServer_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
/* Tell LwIP to associate this structure with this connection. */
tcp_arg(pcb, mem_calloc(sizeof(struct TcpData), 1));
/* Configure LwIP to use our call back functions. */
tcp_err(pcb, TcpServer_conn_err);
tcp_recv(pcb, TcpServer_recv);
return ERR_OK;
}
/**
* @brief Initialize the hello application
* @param None
* @retval None
*/
void TcpServer_init(void)
{
struct tcp_pcb *pcb;
// server_init();
/* Create a new TCP control block */
pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, 8000);
/* Set the connection to the LISTEN state */
pcb = tcp_listen(pcb);
/* Specify the function to be called when a connection is established */
tcp_accept(pcb, TcpServer_accept);
}[/mw_shl_code]
上面的代码连接收发都是正常的。不知道为什么我用tcp测试工具收发数据每次都是 接收到固定的字节数后就收不到了,一定要重新建立连接后才可以。设备不用重启只是测试工具重连就可以。
[/mw_shl_code]
[mw_shl_code=c,true]
[/mw_shl_code]
|