如何使用 TCP (LWIP) 存储接收到的数据?

How to store recieved data using TCP (LWIP)?

我在读取和存储 TCP 服务器接收到的数据时遇到问题。我正在使用 LWIP 库和 NUCLEO-F746ZG 板。我想我必须在执行 es->p 时获取数据。我读过,你必须使用 payload 但我不知道如何在 mi receive 回调中很好地实现它:

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;
}

如果有人知道如何将数据提取为字符串,那将对我有很大帮助。

提前致谢!!

您需要将 pyload memcpy 到您的 char 数组。这是获得有效载荷的唯一方法

char str[somelegth];
memcpy(str, p -> payload, p -> len);