title: LwIP Netconn API + FreeRTOS TCP Client Buffer Issue
body: <p>I've been trying to modify the tcp server example with LwIP in STM32F4DISCOVERY board. I have to write a sender which does not necessarily have to reply server responses. It can send data with 100 ms frequency, for example. </p>
  
  <p>Firstly, the example of TCP server is like this:</p>
  
  <pre><code>static void tcpecho_thread(void *arg)
  {
    struct netconn *conn, *newconn;
    err_t err;
  
    LWIP_UNUSED_ARG(arg);
  
    /* Create a new connection identifier. */
    conn = netconn_new(NETCONN_TCP);
  
    if (conn!=NULL) {  
      /* Bind connection to well known port number 7. */
      err = netconn_bind(conn, NULL, DEST_PORT);
  
      if (err == ERR_OK) {
        /* Tell connection to go into listening mode. */
        netconn_listen(conn);
  
        while (1) {
          /* Grab new connection. */
          newconn = netconn_accept(conn);
  
          /* Process the new connection. */
          if (newconn) {
            struct netbuf *buf;
            void *data;
            u16_t len;
  
            while ((buf = netconn_recv(newconn)) != NULL) {
              do {
                netbuf_data(buf, &amp;data, &amp;len);
  
                //Incoming package
                .....
  
                //Check for data
                if (DATA IS CORRECT) 
                {
                  //Reply
                  data = "OK";
                  len = 2;
  
                  netconn_write(newconn, data, len, NETCONN_COPY);
                }
  
              } while (netbuf_next(buf) &gt;= 0);
  
              netbuf_delete(buf);
            }
  
            /* Close connection and discard connection identifier. */
            netconn_close(newconn);
            netconn_delete(newconn);
          }
        }
      } else {
        printf(" can not bind TCP netconn");
      }
    } else {
      printf("can not create TCP netconn");
    }
  }
  </code></pre>
  
  <p>I modified this code to obtain a client version, this is what I've got so far:</p>
  
  <pre><code>static void tcpecho_thread(void *arg)
  {
    struct netconn *xNetConn = NULL;
  
    struct ip_addr local_ip; 
    struct ip_addr remote_ip; 
    int rc1, rc2; 
  
    struct netbuf *Gonderilen_Buf = NULL;
    struct netbuf *gonderilen_buf = NULL;
    void *b_data;
    u16_t b_len;
  
    IP4_ADDR( &amp;local_ip, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3 );  
    IP4_ADDR( &amp;remote_ip, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );
  
    xNetConn = netconn_new ( NETCONN_TCP );  
    rc1 = netconn_bind ( xNetConn, &amp;local_ip, DEST_PORT ); 
    rc2 = netconn_connect ( xNetConn, &amp;remote_ip, DEST_PORT );
  
    b_data = "+24C"; // Data to be send
    b_len = sizeof ( b_data );  
  
     while(1)
     {
       if ( rc1 == ERR_OK )
        {
          // If button pressed, send data "+24C" to server
          if (GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_0) == Bit_SET)
          {
  
            Buf = netbuf_new();
            netbuf_alloc(Buf, 4); // 4 bytes of buffer
            Buf-&gt;p-&gt;payload = "+24C";
            Buf-&gt;p-&gt;len = 4;
  
            netconn_write(xNetConn, Buf-&gt;p-&gt;payload, b_len, NETCONN_COPY);
            vTaskDelay(100); // To see the result easily in Comm Operator
  
            netbuf_delete(Buf);
          }
      }
      if ( rc1 != ERR_OK || rc2 != ERR_OK )
      {
        netconn_delete ( xNetConn );
      }
    }
  }
  </code></pre>
  
  <p>While the writing operation works, netconn_write sends what's on its buffer. It doesnt care whether <code>b_data</code> is NULL or not. I've tested it by adding the line <code>b_data = NULL;</code></p>
  
  <p>So the resulting output in Comm Operator is like this:</p>
  
  <pre><code>Rec:(02:47:27)+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C
  </code></pre>
  
  <p>However, I want it to work like this:</p>
  
  <pre><code>Rec:(02:47:22)+24C
  Rec:(02:47:27)+24C 
  Rec:(02:57:12)+24C 
  Rec:(02:58:41)+24C
  </code></pre>
  
  <p>The desired write operation happens when I wait for around 8 seconds before I push the button again.</p>
  
  <p>Since netconn_write function does not allow writing to a buffer, I'm not able to clear it. And netconn_send is only allowed for UDP connections.</p>
  
  <p>I need some guidance to understand the problem and to generate a solution for it.
  Any help will be greately appreciated.</p>