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: Ethernet client program development

Part Number: TM4C1294NCPDT

Tool/software: Code Composer Studio

Hi,

I am using the enet_weather example code. I don't want to use that weather application. I want to use this example as a client to connect to my pc as a server and sent some data to my pc having  "IP address" and "port no". How can I start with the development?. I have seen that tcp_echo application but it is related to RTOS. I connected the EVK board(192.168.0.102 ) to a router port and trying to ping. It is not responding.

Pinging 192.168.0.102 with 32 bytes of data:
Reply from 192.168.0.105: Destination host unreachable.
Reply from 192.168.0.105: Destination host unreachable.
Reply from 192.168.0.105: Destination host unreachable.

I tried with the socket related programming by making the lwip_socket as '1' in "lwipopts.h" file.

Regards,

Saiteja.

  • I have forwarded your question to our Ethernet expert. However, this is a major holiday in the United States. He may not be able to respond until late on Monday.
  • Hi,
    The below thread maybe helpful to you.

    e2e.ti.com/.../2687719
  • Hi Charles,

    Thanks for your reply. I got a basic understanding of the given information. In the given link the modified enet_lwip is working as a server. I tried to modify it as a client based on this information http://lwip.wikia.com/wiki/Raw/TCP

    void tcp_setup(void)
    {
    uint32_t data = 0xdeadbeef;

    /* create an ip */
    struct ip_addr ip;

    IP4_ADDR(&ip, 192,168,0,106); // server IP 

    /* create the control block */
    testpcb = tcp_new(); //testpcb is a global struct tcp_pcb

    tcp_bind(testpcb, &ip, 8999);

    tcp_arg(testpcb, &data);

    tcp_err(testpcb, echo_error);
    tcp_recv(testpcb, tcpRecvCallback);
    tcp_sent(testpcb, echo_sent);

    /* now connect */
    tcp_connect(testpcb, &ip, 8999, connectCallback);

    }

    Can you suggest what else I need to work it as a client to send the dat?.

    Regards,

    Saiteja.

  • Hi,

      Below is only an example. The client sends a "Hello World" to the server.

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

  • Hi charles,

    I tried with the given changes. Still, the client is not sending any data to a server, even connection is not established. I have given the server IP address(IP4_ADDR(&server_addr, 192,168,0,105)) and its port number is 8004, which is running in the other pc. the bind call is used without any changes. It is pinging fine but not connected to the server. I commented the echo_init and simply calling the client_init. I put the breakpoint in connectCallback function it is not getting to that breakpoint.

    Can you please summerise which port belongs to which device in the comment lines?. I tried with the usrtcp application same issue arises. I want to know how you tested the client program. In the main also

  • server IP address(IP4_ADDR(&server_addr, 192,168,0,106))
  • What is echo_init and what is client_init? Is that your own code? The code I showed has echo_client_init() instead. If you look at the below two lines in the echo_client_init the port 7000 is the local port to the client and the 8000 is the server port number. Please use wireshark to capture the Ethernet traffic for diagnosis.

    tcp_bind(testpcb, IP_ADDR_ANY, 7000);
    err = tcp_connect(testpcb, &server_addr, 8000, connectCallback);
  • Hi charles,

    It is working now the connection is established between the TM4C(client) and server in other PC and sending the message. It worked when I placed the echo_client _init() function call in the lwIPHostTimerHandler() function. The continuous messages are not sending I tried with tcp_keepalive(). The same code I made it as a server at that I didn't include the server init function in the lwIPHostTimerHandler() function it worked fine. may I know the reason why it is happening?. 

    a part of code snippet in lwIPHostTimerHandler().
    // Display the new IP address.
    //
    UARTprintf("IP Address: ");
    DisplayIPAddress(ui32NewIPAddress);
    UARTprintf("\nOpen a browser and enter the IP address.\n");
    echo_client_init();
    //

    Now I want to make this TCP client as an HTTP client. I have seen the enet_weather example In that they are using their application api's and enet_lwip is also an HTTP server.  I want to send the simple message to the HTTP server and want to print the response from the server. suggest me the best way.

    Regards,

    B T S S Sai Prasad 

  • Hi,
    You are right that you need to wait for the IP address to be acquired from the DHCP server before you can call the echo_client_init. The reason is that if you call the echo_client_init before the IP address is acquired, the client will send a SYNC segment to the server but with the source address equal to 0.0.0.0. The server ill not acknowledge the request if the source address is 0.0.0.0.

    Please take a look at the below link which maybe helpful to answer your question about making a HTTP client request.
    stackoverflow.com/.../how-can-i-send-a-simple-http-request-with-a-lwip-stack
  • Hi Charles,

    Thanks for your reply. Now I want to send the data continuously but it is aborting after one transfer. I am using this code in a while loop to transfer and receive the data continuously.

    while(1)

    {

      tcp_write(testpcb, string, strlen(string), TCP_WRITE_FLAG_COPY);

      tcp_recv(testpcb, echo_recv);

    delay();

    }

    After one one transfer it is getting to the abort() which is in the exit.c file. What changes do I need to implement for continuous transfer.

    Regards,

    B T S S Sai Prasad.

  • Hi,

     Where is your echo_recv() callback? Below is an example.

      

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