中级会员
- 积分
- 344
- 金钱
- 344
- 注册时间
- 2016-8-2
- 在线时间
- 78 小时
|
50金钱
最近在做ETH以太网实验,参考了原子哥的例程,无法发送数据。TCP的回调函数太难读懂了,望各位大神伸出援手
建议TCP_SERVER函数如下
void tcp_echoserver_init(void)
{
/* create new tcp pcb */
tcp_echoserver_pcb = tcp_new();
if (tcp_echoserver_pcb != NULL)
{
err_t err;
/* bind echo_pcb to port 7 (ECHO protocol) */
err = tcp_bind(tcp_echoserver_pcb, IP_ADDR_ANY, 7);
if (err == ERR_OK)
{
/* start tcp listening for echo_pcb */
tcp_echoserver_pcb = tcp_listen(tcp_echoserver_pcb);
/* initialize LwIP tcp_accept callback function */
tcp_accept(tcp_echoserver_pcb, tcp_echoserver_accept);
}
else
{
/* deallocate the pcb */
memp_free(MEMP_TCP_PCB, tcp_echoserver_pcb);
}
}
}
参考了原子哥的例程写的发送函数,始终无法发出,调试发现tcp_echoserver_pcb->callback_arg地址始终为0x0000000,无法实现发送
err_t tcp_server_SendDataPtr(void)
{
struct tcp_echoserver_struct *es;
struct pbuf *ptr;
err_t wr_err = ERR_OK;
es = (struct tcp_echoserver_struct *)tcp_echoserver_pcb->callback_arg;
if(es != NULL)
{
es->p = pbuf_alloc(PBUF_TRANSPORT, strlen((char *)g_tcpserverSendBuffer), PBUF_POOL);
pbuf_take(es->p, (char *)g_tcpserverSendBuffer, 15);
tcp_echoserver_send(tcp_echoserver_pcb, es);
if (es->p != NULL)pbuf_free(es->p);
wr_err = ERR_OK;
}
else
{
tcp_abort(tcp_echoserver_pcb); /* 终止连接,删除pcb控制块 */
wr_err = ERR_ABRT;
}
return wr_err;
}
|
|