This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TM4C1294NCPDT: Continuous data transfer to HTTP server

Part Number: TM4C1294NCPDT


Tool/software: Code Composer Studio

Hi,

I am trying to send the data to http server continuously by using enet_lwip example. I have the http client but after sending the first transfer the client itself closing the connection. I'm creating the http post request and after that calling the  tcp_send_packet() in while loop which is in the main. It is the receving the callback and closing the connection after the first transfer.

err_t tcpRecvCallback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{

UARTprintf("Data recieved.\n");
if (p == NULL) {
UARTprintf("The remote host closed the connection.\n");
UARTprintf("Closing the connection.\n");
tcp_close(testpcb);
return ERR_ABRT;
}
else
{
UARTprintf("Number of pbufs %d\n", pbuf_clen(p));
UARTprintf("Contents of pbuf %s\n", (char *)p->payload);

}

return 0;
}

I tried it in differnet ways. if i put the tcp_setup function in a while along with post request and tcp_send_packet() then it's working by giving error code: -13. can anyone suggest me the right way to send the data continuously to the http server from http client.

Thanks

Regards

B T S S Sai Prasad

  • Hi,
    Can you attach your code? You are just showing a callback function. What are you doing in the tcp_setup and tcp_send_packet?

    BTW, the enet_lwip is a web server application, not a client application. I assume you modified it. 

  • Hi charles,

    I will share the code. please go through it and suggest me the best way to send the data continuously. I'm calling the tcp_setup() function in lwIPHostTimerHandler(void).

    //*****************************************************************************
    //
    // Write data for sending to the remote host...
    //
    //*****************************************************************************
    uint32_t tcp_send_packet(void)
    {

    data_send = false;

    err = tcp_write(testpcb, XmlRequest, strlen(XmlRequest), TCP_WRITE_FLAG_COPY);

    if (err)
    {
    UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_write)\n", err);
    return 1;
    }

    /* now send */
    err = tcp_output(testpcb);

    if (err)
    {
    UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_output)\n", err);
    return 1;
    }

    data_send = true;

    return 0;
    }

    void Create_post_req()
    {
    sprintf( Request,"/%s HTTP/1.1", ServiceMethod);

    sprintf( XmlRequest,"POST %s\r\nHost: %s:%s\r\nContent-Type: application/xml\r\ncache-control: no-cache\r\ncontent-length: %d\r\n\r\n%s\r\n", Request, HostIp,Port,strlen(XmlData),XmlData);

    UARTprintf("\n\nPOST Request which send to the server:\n\n\n");
    UARTprintf("%s", XmlRequest);

    tcp_send_packet();

    }


    //*****************************************************************************
    //
    // Initialize the Callback
    //
    // Used to specify the function that should be called when a TCP connection receives data.
    // Data has been successfully delivered to the remote host..
    //
    //*****************************************************************************
    err_t connectCallback(void *arg, struct tcp_pcb *testpcb, err_t err)
    {

    if (err == ERR_OK)
    {
    UARTprintf("Connection Established.\n");

    tcp_recv(testpcb, tcpRecvCallback);
    tcp_sent(testpcb, NULL);

    UARTprintf("Sending data to the Server\n");

    //tcp_send_packet();


    data_send = true;

    return 0;
    }
    else
    {
    UARTprintf("No Connection Established.\n");
    return 1;
    }
    }

    //*****************************************************************************
    //
    // Initialize the Ethernet client
    //
    // This function initializes all the Ethernet components to not configured.
    //
    // \return None.
    //
    //*****************************************************************************
    void tcp_setup(void)
    {
    /* create an ip */
    struct ip_addr server_addr;


    /* create the control block */
    //testpcb is a global struct tcp_pcb

    testpcb = tcp_new();

    if (testpcb != NULL)
    {
    IP4_ADDR(&server_addr, 89,221,247,3); //IP of the server

    err = tcp_bind(testpcb, IP_ADDR_ANY, 9009); //Binds the connection to a local port number & IP address

    if (err)
    {
    UARTprintf("ERROR: Code: %d (tcp_connect)\n", err);
    }

    /* now connect */
    err = tcp_connect(testpcb, &server_addr, 8089, connectCallback); //Connects to another host.

    if (err)
    {
    UARTprintf("ERROR: Code: %d (tcp_connect)\n", err);
    }

    UARTprintf("PCB CREATED\n");

    }
    else
    {
    memp_free(MEMP_TCP_PCB, testpcb);
    UARTprintf("PCB NOT CREATED\n");

    }

    }

    main()

    {

    ..........................

    .........................

    while(1)
    {
    if(data_send == true)
    {
    Create_post_req();

    //tcp_write(testpcb, XmlRequest, strlen( XmlRequest), TCP_WRITE_FLAG_COPY);

    // tcp_output(testpcb);

    SysCtlDelay(g_ui32SysClock / 10 / 3);

    //tcp_setup();
    }

    }

    }

    Thanks

    Regards

    B T S S Sai Prasad.

  • Hi,

     This is an example for the client which echoes backs what is received from the server. Can you take a look?

    #include <stdbool.h>
    #include <stdint.h>
    #include <string.h>
    #include <stdio.h>
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/flash.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/gpio.h"
    #include "driverlib/rom_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/systick.h"
    #include "inc/hw_emac.h"
    
    #include "utils/lwiplib.h"
    #include "utils/ustdlib.h"
    #include "utils/uartstdio.h"
    #include "drivers/pinout.h"
    
    #include "lwip/debug.h"
    #include "lwip/stats.h"
    #include "lwip/tcp.h"
    
    
    
    extern void echo_init(void);
    
    err_t echo_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
    err_t echo_poll(void *arg, struct tcp_pcb *tpcb);
    void echo_error(void *arg, err_t err);
    err_t echo_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
    err_t err;
    
    uint32_t tcp_send_packet(void);
    
    static struct tcp_pcb *testpcb;  //pcb global
    
    
    #define SYSTICKHZ               100
    #define SYSTICKMS               (1000 / SYSTICKHZ)
    
    #define SYSTICK_INT_PRIORITY    0x80
    #define ETHERNET_INT_PRIORITY   0xC0
    
    uint32_t g_ui32IPAddress;
    
    uint32_t g_ui32SysClock;
    volatile bool g_bLED;
    bool g_bIPAddrValid = 0;
    
    
    
    //*****************************************************************************
    // The error routine that is called if the driver library encounters an error.
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, uint32_t ui32Line)
    {
    }
    #endif
    
    
    void
    DisplayIPAddress(uint32_t ui32Addr)
    {
        char pcBuf[16];
        // Convert the IP Address into a string.
         usprintf(pcBuf, "%d.%d.%d.%d", ui32Addr & 0xff, (ui32Addr >> 8) & 0xff,
                (ui32Addr >> 16) & 0xff, (ui32Addr >> 24) & 0xff);
        UARTprintf(pcBuf);
    }
    
    //*****************************************************************************
    // Required by lwIP library to support any host-related timer functions.
    //*****************************************************************************
    void
    lwIPHostTimerHandler(void)
    {
        uint32_t ui32NewIPAddress;
    
        ui32NewIPAddress = lwIPLocalIPAddrGet(); // Get the current IP address.
    
        if(ui32NewIPAddress != g_ui32IPAddress) // See if the IP address has changed.
        {
            if(ui32NewIPAddress == 0xffffffff)
            {
                UARTprintf("Waiting for link.\n");
            }
            else if(ui32NewIPAddress == 0)
            {
                UARTprintf("Waiting for IP address.\n");
            }
            else
            {
            	g_bIPAddrValid = 1;
                UARTprintf("IP Address: ");
                DisplayIPAddress(ui32NewIPAddress);
                UARTprintf("\n\n");
            }
            g_ui32IPAddress = ui32NewIPAddress;
        }
    
        if((ui32NewIPAddress == 0) || (ui32NewIPAddress == 0xffffffff))
        {
            //
            // Do nothing and keep waiting.
            //
        }
    }
    
    //*****************************************************************************
    // The interrupt handler for the SysTick interrupt.
    //*****************************************************************************
    void
    SysTickIntHandler(void)
    {
        lwIPTimer(SYSTICKMS);
    
         g_bLED = true;
    }
    
    
    //*****************************************************************************
    char mydata[1024];
    
    err_t echo_receive(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
    {
        int len;
        char *header = "Client: ";
        int header_len = strlen(header);
    
        UARTprintf("Data received.\n");
        if (p == NULL) {
            UARTprintf("The remote host closed the connection.\n");
            tcp_close(tpcb);
            return ERR_ABRT;
        } else {
            tcp_recved(tpcb, p->tot_len);
    
            //size of the pay load
            len = p->tot_len + header_len;
    
            strcpy(mydata, header);
            strcat(mydata, (char *)p->payload);
    
    
           //Free the packet buffer
            pbuf_free(p);
    
            //check output buffer capacity
            if (len > tcp_sndbuf(tpcb))
            	len= tcp_sndbuf(tpcb);
    
            //Enqueue the data to send out
            //err = tcp_write(tpcb, mydata, len, 0);
            err = tcp_write(tpcb, mydata, len, 0);
            if (err == ERR_OK) {
                UARTprintf("Echo the data out\n");
            }
            else if(err == ERR_MEM)
            {
                /* we are low on memory, try later, defer to poll */
                UARTprintf("Low on memory.\n");
    
            } else
            {
               /* Other problems to take care of */
                UARTprintf("Other problems.\n");
            }
            tcp_sent(tpcb, NULL); /* No need to call back */
        }
    
    
        return ERR_OK;
    }
    
    uint32_t tcp_send_packet(void)
    {
        char *string = "Hello World! \n\r ";
    
        err = tcp_write(testpcb, string, strlen(string), TCP_WRITE_FLAG_COPY);
    
    
        if (err) {
            UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_write)\n", err);
            return 1;
        }
    
        /* now send */
        err = tcp_output(testpcb);
        if (err) {
            UARTprintf("ERROR: Code: %d (tcp_send_packet :: tcp_output)\n", err);
            return 1;
        }
        return 0;
    }
    
    err_t connectCallback(void *arg, struct tcp_pcb *tpcb, err_t err)
    {
    
    
        if (err == ERR_OK){
    
    		UARTprintf("Connection Established.\n");
    
    
    		tcp_recv(testpcb, echo_receive);
    		tcp_sent(testpcb, NULL);
    		UARTprintf("Sending a greeting message to the Server\n");
    		tcp_send_packet();
    		return 0;
        } else {
    		UARTprintf("No Connection Established.\n");
    		return 1;
    
        }
    }
    
    void echo_client_init(void)
    {
        /* create an ip */
        struct ip_addr server_addr;
    
    
        /* create the control block */
        testpcb = tcp_new();    //testpcb is a global struct tcp_pcb
    
        if (testpcb != NULL)
        {
            IP4_ADDR(&server_addr, 10,219,15,38);    //IP of the PC computer
    
    		tcp_bind(testpcb, IP_ADDR_ANY, 7000);
    
    		/* now connect */
    		err = tcp_connect(testpcb, &server_addr, 8000, connectCallback);
    	    if (err) {
    	        UARTprintf("ERROR: Code: %d (tcp_connect)\n", err);
    	    }
    
    		UARTprintf("PCB CREATED\n");
    
        } else {
            memp_free(MEMP_TCP_PCB, testpcb);
    		UARTprintf("PCB NOT CREATED\n");
    
        }
    
    }
    
    
    int
    main(void)
    {
        uint32_t ui32User0, ui32User1;
        uint8_t pui8MACArray[8];
    
    
        SysCtlMOSCConfigSet(SYSCTL_MOSC_HIGHFREQ);
    
        g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                                 SYSCTL_OSC_MAIN |
                                                 SYSCTL_USE_PLL |
                                                 SYSCTL_CFG_VCO_480), 120000000);
        PinoutSet(true, false);
    
        UARTStdioConfig(0, 115200, g_ui32SysClock);
    
    
        UARTprintf("\033[2J\033[H");
        UARTprintf("Ethernet lwIP TCP echo client example\n\n");
    
        MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_1);
    
        MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1, ~GPIO_PIN_1);
    
        MAP_SysTickPeriodSet(g_ui32SysClock / SYSTICKHZ);
        MAP_SysTickEnable();
        MAP_SysTickIntEnable();
    
        MAP_FlashUserGet(&ui32User0, &ui32User1);
        if((ui32User0 == 0xffffffff) || (ui32User1 == 0xffffffff))
        {
            UARTprintf("No MAC programmed!\n");
            while(1)
            {
            }
        }
    
        UARTprintf("Waiting for IP.\n");
    
        pui8MACArray[0] = ((ui32User0 >>  0) & 0xff);
        pui8MACArray[1] = ((ui32User0 >>  8) & 0xff);
        pui8MACArray[2] = ((ui32User0 >> 16) & 0xff);
        pui8MACArray[3] = ((ui32User1 >>  0) & 0xff);
        pui8MACArray[4] = ((ui32User1 >>  8) & 0xff);
        pui8MACArray[5] = ((ui32User1 >> 16) & 0xff);
    
        lwIPInit(g_ui32SysClock, pui8MACArray, 0, 0, 0, IPADDR_USE_DHCP);
    
    
        while (g_bIPAddrValid == 0);
    
    
        echo_client_init();
    
    
        MAP_IntPrioritySet(INT_EMAC0, ETHERNET_INT_PRIORITY);
        MAP_IntPrioritySet(FAULT_SYSTICK, SYSTICK_INT_PRIORITY);
    
    
    
        //
        // Loop forever, processing the LED blinking.  All the work is done in
        // interrupt handlers.
        while(1)
        {
    
            while(g_bLED == false)
            {
            }
    
            //
            // Clear the flag.
            //
            g_bLED = false;
    
            //
            // Toggle the LED.
            //
            MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1,
                             (MAP_GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_1) ^
                              GPIO_PIN_1));
        }
    }
    

  • Hi Charles,

    The same connection closing scenario is repeating with the given inputs. In our case, the HTTP client will get the continuous data and it will be transmitted to the HTTP server continuously, the server will send the 200 ok response for every transmission of data. I have observed for the first time it is sending the data after that it is getting terminating. I have observed the flow by putting a breakpoint it the echo_receiv().

    In the first call, it is verifying the 'p == NULL in echo_receive(). It comes to the TCP_EVENT_RECV(PCB, recv_data, ERR_OK, err); which is in the tcp_in.c file and sending the data to the HTTP server.

    In the second call,it is verifying the 'p == NULL in echo_receive(). It comes to the TCP_EVENT_RECV(PCB, recv_data, ERR_OK, err); which is in the tcp_in.c file but not sending the data to the server.

    In the third call, it is verifying the 'p == NULL in echo_receive(). It comes to the TCP_EVENT_CLOSED(PCB, err); which is in the tcp_in.c file closing the connection.

    In the fourth call tcp_write() error is generating.

       

    I'm unable to understand this why it is aborting the connection after the 3rd time and sending the data in the first call. Can you explain to me how to control the flow for the continuous transmission between the HTTP client and the HTTP Server?.

    Thanks

    Regards

    B T S S Sai Prasad.

  • Hi,
    Is this issue resolved? Are you able to make some progress and hopefully you are able to resolve on your own as I'm unable to figure out what was really wrong. I will also suggest that you seek some advice from LwIP support team on this as they are the experts on the LwIP stacks. I will close this thread for now. If you have new questions you can open a new thread. If you have some updates to this post later on you can just reopen this thread by posting comments.
  • Hi Charles,

    Yes, it was rectified on my own. I found that there is an issue with the request format. It is a simple \r\n at the end of the request. I don't how it is terminating the connection(\r\n)?. After removing that it is working fine. if you know the answer let me know.

    Thanks,

    Regards

    B T S S Sai Prasad.