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.

TM4C129XNCZAD: Issue with HTTP server with port number 8000

Part Number: TM4C129XNCZAD

I am trying to create a server on my device that communicates with HTTP over port 8000 using the code with these configurations as follows for vNetworkConfigurationTask

```
Clock_Params_init(&instanceParams);
instanceParams.startFlag = STD_TRUE;
instanceParams.period = 100;
(void)Clock_create(llTimerTick, instanceParams.period, &instanceParams, NULL);

NC_SystemOpen((INT32S)NC_PRIORITY_LOW, (INT32S)NC_OPMODE_INTERRUPT);

hCfgIpAddr = CfgNew();

Web_vAddFiles();

/* Setup manual IP address */
CfgAddEntry(hCfgIpAddr, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 0U, (INT32S)strlen(s8HostNameArray), (INT8U *)(s8HostNameArray), STD_NULL);
bzero(&objstrIPNetType, sizeof(objstrIPNetType));
CfgAddEntry(hCfgIpAddr, CFGTAG_IPNET, (INT32U)1, 0U, (INT32S)sizeof(CI_IPNET), (INT8U *)&objstrIPNetType, STD_NULL);
CfgAddEntry(hCfgIpAddr, CFGTAG_ROUTE, 0U, 0U, (INT32S)sizeof(CI_ROUTE), (INT8U *)&objstrRouteType, STD_NULL);

/* Specify HTTP service */

CI_SERVICE_HTTP objstrHTTPServiceType;
bzero(&objstrHTTPServiceType, sizeof(objstrHTTPServiceType));
objstrHTTPServiceType.cisargs.IPAddr = inet_addr("INADDR_ANY");
objstrHTTPServiceType.cisargs.pCbSrv = SoAd_vNetworkStatusChangeHook;
objstrHTTPServiceType.cisargs.IfIdx = 1;
objstrHTTPServiceType.param.MaxCon = 8;
objstrHTTPServiceType.param.Port = 8000;

CfgAddEntry(hCfgIpAddr, \
    CFGTAG_SERVICE, \
    CFGITEM_SERVICE_HTTP, \
    0U,\
    (INT32S)sizeof(objstrHTTPServiceType), \
    (INT8U *)&objstrHTTPServiceType, \
    STD_NULL);

/*init TCP config*/

s32TransmitBufSize = 1024;
     (void)CfgAddEntry(hCfgIpAddr, \
     CFGTAG_IP, \
     CFGITEM_IP_SOCKTCPTXBUF,\
     CFG_ADDMODE_UNIQUE, \
     (INT32S)sizeof(INT32U), \
     (INT8U *)&s32TransmitBufSize, \
     STD_NULL);

s32ReceiveBufSize = 1024;
(void)CfgAddEntry(hCfgIpAddr, \
     CFGTAG_IP, \
     CFGITEM_IP_SOCKTCPRXBUF,\
     CFG_ADDMODE_UNIQUE, \
     (INT32S)sizeof(INT32U), \
     (INT8U *)&s32ReceiveBufSize, \
     STD_NULL);
s32ReceiveBufLimit = 2048;
(void)CfgAddEntry(hCfgIpAddr, \
     CFGTAG_IP, \
     CFGITEM_IP_SOCKTCPRXLIMIT,\
     CFG_ADDMODE_UNIQUE, \
     (INT32S)sizeof(INT32U), \
     (INT8U *)&s32ReceiveBufLimit, \
     STD_NULL);


/* add the configuration settings for NDK low-priority tasks stack size. */
s32NewValue = 1200;
(void)CfgAddEntry(hCfgIpAddr, \
     CFGTAG_OS, \
     CFGITEM_OS_TASKSTKLOW,\
     CFG_ADDMODE_UNIQUE, \
     (INT32S)sizeof(INT32U), \
     (INT8U *)&s32NewValue, \
     STD_NULL);
/* add the configuration settings for NDK norm priority tasks stack size. */
s32NewValue = 1024;
(void)CfgAddEntry(hCfgIpAddr, \
     CFGTAG_OS, \
     CFGITEM_OS_TASKSTKNORM,\
     CFG_ADDMODE_UNIQUE, \
     (INT32S)sizeof(INT32U), \
     (INT8U *)&s32NewValue, \
     STD_NULL);
/* add the configuration settings for NDK high priority tasks stack size. */
s32NewValue = 2048;
(void)CfgAddEntry(hCfgIpAddr, \
     CFGTAG_OS, \
     CFGITEM_OS_TASKSTKHIGH,
     CFG_ADDMODE_UNIQUE, \
     (INT32S)sizeof(INT32U), \
     (INT8U *)&s32NewValue, \
     STD_NULL);

do
{
    s32ReturnStatus = NC_NetStart(hCfgIpAddr,\
    SoAd_vNetworkOpenHook,\
    SoAd_vNetworkCloseHook,\
    SoAd_vNetworkIPAddressChangeHook);
}while(s32ReturnStatus > 0);
/* call user defined stack delete hook */
Web_vRemoveFiles();
/* Delete Configuration */
CfgFree(hCfgIpAddr);
/* Close the OS */
NC_SystemClose();

```
and added web files add and remove hooks as follows

```
addWebFiles(){
    efs_createfile("index.html", INDEX_SIZE, (INT8U *)INDEX);
    efs_createfile("Web_u8WebServerCGI.cgi", 0, (INT8U *)Web_u8WebServerCGI);
}

removeWebFiles(){
     efs_destroyfile("index.html");
     efs_destroyfile("Web_u8WebServerCGI.cgi");
}

```
and set the priorities of NDK tasks as follows

```
Global.lowTaskPriLevel = 8;
Global.normTaskPriLevel = 10;
Global.highTaskPriLevel = 12;
Global.kernTaskPriLevel = 14;
```
and I got an ERROR 
net::ERR_INVALID_HTTP_RESPONSE



and I tried to find out why this error appeared so I used Wireshark to list all the traffic between the server(10.1.0.204) my device and client(10.1.0.203) my PC 

No. Time Source Destination Protocol Length Info
4712 133.8418 10.1.0.203 10.1.0.204 TCP 66 57911  >  8000 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 WS=256 SACK_PERM
4715 133.8427 10.1.0.204 10.1.0.203 TCP 60 8000  >  57911 [SYN, ACK] Seq=0 Ack=1 Win=1024 Len=0 MSS=1460
4716 133.8428 10.1.0.203 10.1.0.204 TCP 54 57911  >  8000 [ACK] Seq=1 Ack=1 Win=64240 Len=0
4717 133.843 10.1.0.203 10.1.0.204 HTTP 496 POST /Web_u8WebServerCGI.cgi HTTP/1.1  (text/plain)
4718 133.8447 10.1.0.204 10.1.0.203 TCP 60 8000  >  57911 [ACK] Seq=1 Ack=443 Win=582 Len=0
4719 133.948 10.1.0.204 10.1.0.203 TCP 61 8000  >  57911 [PSH, ACK] Seq=1 Ack=443 Win=1024 Len=7 [TCP segment of a reassembled PDU]
4720 133.9481 10.1.0.204 10.1.0.203 TCP 60 8000  >  57911 [PSH, ACK] Seq=8 Ack=443 Win=1024 Len=3 [TCP segment of a reassembled PDU]
4721 133.9482 10.1.0.203 10.1.0.204 TCP 54 57911  >  8000 [ACK] Seq=443 Ack=11 Win=64230 Len=0
4722 133.9485 10.1.0.204 10.1.0.203 TCP 60 8000  >  57911 [PSH, ACK] Seq=11 Ack=443 Win=1024 Len=3 [TCP segment of a reassembled PDU]
4723 133.9485 10.1.0.203 10.1.0.204 TCP 54 57911  >  8000 [FIN, ACK] Seq=443 Ack=11 Win=64230 Len=0
4724 133.9485 10.1.0.203 10.1.0.204 TCP 54 57911  >  8000 [RST, ACK] Seq=444 Ack=14 Win=0 Len=0
4725 133.9488 10.1.0.204 10.1.0.203 TCP 60 8000  >  57911 [PSH, ACK] Seq=14 Ack=443 Win=1024 Len=3 [TCP segment of a reassembled PDU]
4726 133.9488 10.1.0.203 10.1.0.204 TCP 54 57911  >  8000 [RST] Seq=443 Win=0 Len=0
4727 133.9491 10.1.0.204 10.1.0.203 TCP 60 8000  >  57911 [PSH, ACK] Seq=17 Ack=443 Win=1024 Len=3 [TCP segment of a reassembled PDU]
4728 133.9491 10.1.0.203 10.1.0.204 TCP 54 57911  >  8000 [RST] Seq=443 Win=0 Len=0
4729 133.9493 10.1.0.204 10.1.0.203 TCP 60 8000  >  57911 [PSH, ACK] Seq=20 Ack=443 Win=1024 Len=3 [TCP segment of a reassembled PDU]
4730 133.9493 10.1.0.203 10.1.0.204 TCP 54 57911  >  8000 [RST] Seq=443 Win=0 Len=0
4731 133.9497 10.1.0.204 10.1.0.203 TCP 60 8000  >  57911 [PSH, ACK] Seq=23 Ack=443 Win=1024 Len=3 [TCP segment of a reassembled PDU]
4732 133.9497 10.1.0.203 10.1.0.204 TCP 54 57911  >  8000 [RST] Seq=443 Win=0 Len=0
4733 133.9604 10.1.0.204 10.1.0.203 TCP 513 8000  >  57911 [PSH, ACK] Seq=26 Ack=444 Win=1024 Len=459 [TCP segment of a reassembled PDU]
4734 133.9604 10.1.0.203 10.1.0.204 TCP 54 57911  >  8000 [RST] Seq=444 Win=0 Len=0

so from this table, I find out that the server continues sending the packets without an acking to the packets sent with the client

also my TI-RTOS version is 2.16.1.14
which used NDK version 2_25_00_09

and I notice that the GET request for the HTML (index.html) is happening successfully with the specified port 8000 but requesting POST /Web_u8WebServerCGI.cgi have an issue 
I also notice that the Web_u8WebServerCGI is being called successfully and the response is created and passed to the NDK successfully but this error ERR_INVALID_HTTP_RESPONSE arises and the client is failed to parse the response.

but with port 80 everything is working with no problem

so how can I solve this issue?

  • Hi,

     I'm currently out of office. I will look back to your problem tomorrow. Sorry for the delayed response.

  • Thank you, Charles, for your answer.

    After thoroughly checking all XDC configurations and reviewing my code, I haven't been able to determine the exact cause of the problem. However, I did make some progress. I had a functional HTML and JavaScript sample code that worked on both ports 80 and 8089, following the same principles and using web sockets.

    When I replaced the sample code with my original HTML and JavaScript files, I encountered an issue where the code worked correctly on port 80. The CGI successfully received and responded to the data. However, when testing with Chrome and Postman, I received the following error: net::ERR_INVALID_HTTP_RESPONSE.

    To investigate further, I used Wireshark to analyze the TCP messages on the Ethernet. I observed that the requests sent and the responses received were exactly the same.

    Despite these efforts, I haven't been able to identify the specific cause of the net::ERR_INVALID_HTTP_RESPONSE error. I have checked the XDC configurations, reviewed the code, and analyzed the network traffic. At this point but still can't figure out the cause.

  • the board also responded correctly on the network to port 8080 as it did with port 80 but the client terminates the connection and issued this error  net::ERR_INVALID_HTTP_RESPONSE  even though the data exists or that is what I see on the ethernet connection using Wireshark also I tried to add connection rules that allow incoming and outgoing connections on the port 8080 so if the firewall blocking it or something but the issue persists.

  • Hi Hazem,

      I don't know what is causing the problem. 

    I had a functional HTML and JavaScript sample code that worked on both ports 80 and 8089, following the same principles and using web sockets.

    I assume you have run this functional HTML and JS sample code on the MCU, correct? Or this is run on a different platform?

    When I replaced the sample code with my original HTML and JavaScript files, I encountered an issue where the code worked correctly on port 80.

    What is the exact difference between the functional sample code and your original HTML and JS?

    However, when testing with Chrome and Postman, I received the following error: net::ERR_INVALID_HTTP_RESPONSE.

    To investigate further, I used Wireshark to analyze the TCP messages on the Ethernet. I observed that the requests sent and the responses received were exactly the same.

    First, the ERR_INVALID_HTTP_RESPONSE is an issue caused by the response from the client to the server, correct?

    Can you compare the requests and responses in more detail by comparing the packets byte by byte. Are they exactly the same between the sample code and your own code?

    the board also responded correctly on the network to port 8080 as it did with port 80 but the client terminates the connection and issued this error  net::ERR_INVALID_HTTP_RESPONSE

    Can you clarify one more time, is this error message what you see on the PC browser for the client side? Or this is what you see on the CCS console when the client fails to respond? 

    Have you tried different network such as your home network? I wonder if it makes a difference on a different network. If you have a switch, you can configure the switch to do a port forwarding so that Wireshark can capture everything that is sent/received to the MCU. 

  • the html and js codes are on the MCU and requested correctly except for the CGI request that I make from the Chrome console as follows

    fetch("http://10.1.0.204:8080/Web_u8WebServerCGI.cgi", {
      method: "POST",
      headers: {
        "Content-Type": "text/plain;charset=UTF-8",
        "Authorization": "Basic YWRtaW46YWRtaW4="
      },
      body: "Mode=1,CMD=18,0,"
    })
      .then(response => {
        if (response.ok) {
          console.log("Ready");
          return response.text();
        } else {
          console.log("Error");
        }
      })
      .then(data => {
        console.log(data);
      })
      .catch(error => {
        console.log("Error:", error);
      });



    and the CGI function is

    static INT8U Web_u8WebServerCGI(void *pvCGISocketData, INT32S s32RXMsgLength)
    {
        vValidateCGISocketInfo(pvCGISocketData, &objstrWebSocketInfo);
        httpSendClientStr(pvCGISocketData, "[[32],[82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],[102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119],[[39,\"F\"]],[[60,\"Board Settings\",0,0,\"(111)\",\"sb18\",0,0,1],[0,0,5,\"b1\",\"Serial Number\",1,0,1,21285772,5,\"b2\",\"Firmware Version\",1,1,1,\"02.00.af.00.00.00.00\",5,\"b3\",\"MAC Address\",1,1,1,\"c4-7c-8d-00-0d-1e\",5,\"b4\",\"Downloading XML Version\",1,1,1,\"9.4.21.2.2.1\",5,\"b5\",\"Protocol Number\",1,1,1,\"6.11.20.9.17.2\",5,\"b6\",\"Configuration Version\",1,1,1,3]]]");
        
        return (1);
    }


    also, the CGI function takes about 200 ms to respond and prepare this string 
    and all NDK stack is configured with IP:10.1.0.204 and httpPort: 80 which I change at run time and restart the NDK
    to load with new port settings


    I got this net::ERR_INVALID_HTTP_RESPONSE error on Chrome console after execution of JS code the same behavior happens when I send the CGI request using Postman

    I don't think the issue is in the content itself as it works correctly with port 80 but 

    I compared the requests in case of port 80 (working) and 8080 (not working !!)

    here is the ASCII message for port 80

    request sent from WebClient to the server is in RED
    and sent from the server to WebClient is in BLUE

    POST /Web_u8WebServerCGI.cgi HTTP/1.1
    Host: 10.1.0.204
    Connection: keep-alive
    Content-Length: 16
    Authorization: Basic YWRtaW46YWRtaW4=
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36
    Content-Type: text/plain;charset=UTF-8
    Accept: */*
    Origin: http://10.1.0.204
    Referer: http://10.1.0.204/
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.9,ar;q=0.8
    Mode=1,CMD=18,0,
    [[32],[82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],[102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119],[[39,"F"]],[[60,"Board Settings",0,0,"(null)","sb18",0,0,1],[0,0,5,"b1","Serial Number",1,0,1,21285772,5,"b2","Firmware Version",1,1,1,"02.00.af.00.00.00.00",5,"b3","MAC Address",1,1,1,"c4-7c-8d-00-0d-1e",5,"b4","Downloading XML Version",1,1,1,"9.4.21.2.2.1",5,"b5","Protocol Number",1,1,1,"6.11.20.9.17.2",5,"b6","Configuration Version",1,1,1,3]]]

    and here is the message for port 8080

    POST /Web_u8WebServerCGI.cgi HTTP/1.1Host: 10.1.0.204:8080
    Connection: keep-alive
    Content-Length: 16
    Authorization: Basic YWRtaW46YWRtaW4=
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36
    Content-Type: text/plain;charset=UTF-8
    Accept: */*
    Origin: http://10.1.0.204:8080
    Referer: http://10.1.0.204:8080/
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.9,ar;q=0.8
    Mode=1,CMD=18,0,[[32],[82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],[102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119],[[39,"F"]],[[60,"Board Settings",0,0,"(null)","sb18",0,0,1],[0,0,5,"b1","Serial Number",1,0,1,21285772,5,"b2","Firmware Version",1,1,1,"02.00.af.00.00.00.00",5,"b3","MAC Address",1,1,1,"c4-7c-8d-00-0d-1e",5,"b4","Downloading XML Version",1,1,1,"9.4.21.2.2.1",5,"b5","Protocol Number",1,1,1,"6.11.20.9.17.2",5,"b6","Configuration Version",1,1,1,3]]]


    they are the same and on the TCP requests level I think they would be different as there are many TCP packets sent to terminate the connection which I copied earlier in previous comments with the [RST] tag

    also, I tried other networks, directly connect the Ethernet of the MCU to PC, and used my home router also used wireshark with such filter ip.src== 10.1.0.204 || ip.dst==10.1.0.204

    to find out requests coming to and from the MCU with this IP 10.1.0.204 

  • Hi, 

      I'm still not sure what is the problem. However, I simply do a beyond compare diff between the two REDS and the two BLUES. The two BLUES are identical. But there is a subtle difference between the two REDS. See below. If you look at the first line for 8080 port, the POST command and the Host are concatenated into the same line. I will suggest you break it into two lines just like what you have for port 80 and see if that makes a difference. 

  • they are the same even the two REDS except for the port number but with the logs I sent earlier there is a timing issue with different ports than port 80 for HTTP requests the behavior is that the MCU continues sending packets of data (a response which is the BLUE text) even the client sends TCP packet with tag [RST] which terminates the connection and the MCU doesn't listen to that command and continue sending data 

    - the first thing is that I don't know why the client fires the command [RST] with port 8080 before all data packets are sent and don't with port 80 even though all settings for my PC allow connections on port 8080 (I added a rule to allow connections on this port from windows firewall settings incoming and outgoing connections)

    that's why I see the same request and response on both ports 80 and 8080 but the application layer (my client (chrome console, js code that sends the request) ) detects that command of [RST] that terminates the connection before receiving all data on TCP packets Level with port 8080 

    which triggers a corruption in the request itself and fires the error net::ERR_INVALID_HTTP_RESPONSE on the Chrome console

    and with port 80 this TCP packet of [RST] is sent after all TCP data packets are sent and terminates the connection correctly which will work fine with port 80

    the second thing is why the MCU doesn't terminate the connection upon reception of this [RST] packet and continues sending data even though the client is requesting to terminate.

  • Hi,

    even the client sends TCP packet with tag [RST] which terminates the connection and the MCU doesn't listen to that command and continue sending data 

      I don't know why the client is sending a RST instead of FIN to terminate a connection. RST is more like a immediate abort rather than a graceful connection close as in FIN. This article will help to understand the basics between RST and FIN. 

    https://www.baeldung.com/cs/tcp-fin-vs-rst#:~:text=An%20RST%20message%20aborts%20a%20TCP%20connection.&text=In%20practice%2C%20an%20RST%20message,from%20the%20other%20entity%20communicating.

    the second thing is why the MCU doesn't terminate the connection upon reception of this [RST] packet and continues sending data even though the client is requesting to terminate.

    Here is another discussion that talks about using SO_LINGER with timeout of 0 that immediately close the connection. Try with SO_LINGER and see if that makes a difference. 

    https://stackoverflow.com/questions/3757289/when-is-tcp-option-so-linger-0-required

  • I think this article also helps why RST is being sent. 

    https://iponwire.com/tcp-reset-rst-reasons/

  • Finally, The problem was Solved !!!

    Thanks, Charles for your help the links you sent earlier were accommodating actually and helped me understand how the connection is established on the TCP level 

    The problem was that the application layer is configured to understand and parse default ports for HTTP (80), HTTPS (443), and other default protocols. This means that it would understand the data sent even if no marker on the data indicating the protocol version or content type. However, when I changed the port to a custom HTTP port (8080), this default behavior did not happen as the application layer doesn't know which protocol is used. In order for the application layer on the client side to understand the data, I needed to add this information. I solved the problem by adding two lines before sending any data. This allowed me to serve any custom port other than port 80. The same approach can be used with any different protocol, such as HTTPS.

    httpSendStatusLine(objstrWebSocketInfo.objSocket, HTTP_OK, CONTENT_TYPE_HTML);
    httpSendClientStr(objstrWebSocketInfo.objSocket, CRLF);
    
    //then send whatever you want :D


    so for the same request, we were talking about earlier, it would be like

    POST /Web_u8WebServerCGI.cgi HTTP/1.1
    Host: 10.1.0.204:8094
    Connection: keep-alive
    Content-Length: 16
    Authorization: Basic YWRtaW46YWRtaW4=
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36
    Content-Type: text/plain;charset=UTF-8
    Accept: */*
    Origin: http://10.1.0.204:8094
    Referer: http://10.1.0.204:8094/
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.9,ar;q=0.8
    Mode=1,CMD=18,0,
    HTTP/2.0 200 OK
    Content-type: text/html
    [[32],[82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],[102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119],[[39,"F"]],[[60,"Board Settings",0,0,"(null)","sb18",0,0,1],[0,0,5,"b1","Serial Number",1,0,1,21285772,5,"b2","Firmware Version",1,1,1,"02.00.b9.00.00.00.00",5,"b3","MAC Address",1,1,1,"c4-7c-8d-00-0d-1e",5,"b4","Downloading XML Version",1,1,1,"9.4.21.2.2.1",5,"b5","Protocol Number",1,1,1,"6.11.20.9.17.2",5,"b6","Configuration Version",1,1,1,3]]]

    where RED is the request sent from Client to server and Blue would be the response sent from server to client