简体   繁体   English

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

[英]How to store recieved data using TCP (LWIP)?

I've got problems to read and store the received data by a TCP server. 我在通过TCP服务器读取和存储接收到的数据时遇到问题。 I'm using the LWIP library and the NUCLEO-F746ZG board. 我正在使用LWIP库和NUCLEO-F746ZG开发板。 I suppose that I have to get the data when I do es->p . 我想我做es-> p时必须获取数据。 I've read that, you have to use the payload but I don't know how to implement it well in mi receive callback: 我读过,您必须使用有效负载,但我不知道如何在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;
}

If anyone knows how to extract the data as a string it would help me a lot. 如果有人知道如何将数据提取为字符串,那将对我有很大帮助。

thank you in advance!! 先感谢您!!

You need to memcpy the pyload to your char array. 您需要将pyload存入char数组。 That is the only way of getting the payload 那是获得有效载荷的唯一方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM