我在通过TCP服务器读取和存储接收到的数据时遇到问题。我正在使用 LWIP 库和NUCLEO-F746ZG开发板。我想我必须 es-> p 时获取数据。我读过,您必须使用有效载荷,但我不知道如何在mi接收回调中很好地实现它:
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;
u32_t *tempPtr;
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);
}
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)
{
/* first data chunk in p->payload */
es->state = ES_RECEIVED;
/* store reference to incoming pbuf (chain) */
es ->p = p; //I can´t convert this into a String
/* initialize LwIP tcp_sent callback function */
tcp_sent(tpcb, tcp_echoserver_sent);
/* send back the received data (echo) */
tcp_echoserver_send(tpcb, es);
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_echoserver_send(tpcb, es);
}
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;
}
else if(es->state == ES_CLOSING)
{
/* odd case, remote side closing twice, trash data */
//tcp_recved(tpcb, p->tot_len);
//es->p = NULL;
//pbuf_free(p);
ret_err = ERR_OK;
}
else
{
/* unkown es->state, trash data */
//tcp_recved(tpcb, p->tot_len);
//es->p = NULL;
//pbuf_free(p);
ret_err = ERR_OK;
}
return ret_err;
}
如果有人知道如何将数据提取为字符串,这将对我有很大帮助。
先谢谢您!
答案 0 :(得分:2)
您需要将pyload存入char数组。那是获取有效载荷的唯一方法
char str[somelegth];
memcpy(str, p -> payload, p -> len);