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.

CC3220SF: HTTPClient Error -10

Part Number: CC3220SF


Hi,

I am using the CC3220SF with SDK Version 2.40.02.00 and using the httpclient to connect post data to a server at a set rate. I've successfully gotten the device to post my data at my desired frequency, however after a certain amount of posts I begin to get error -10 preventing me from posting. According to slneterr.h this error is "SLNETERR_BSD_ENSOCK" which is "The system limit on the total number of open sockets, has been reached." This value is always returned from the HTTPClient_sendRequest or HTTPClient_connect function calls in my code. This leads me to believe that the sockets opened by the httpclient.c functions are not being properly closed, even though the HTTPClient_disconnect is supposed to close the socket that is used in the HTTPClient_Handle passed into it. I've added my code to this post, I have it set to run every minute. I'd appreciate any help I could get with this issue!

HTTPClient_Handle httpClientHandle;            
bool moreDataFlag = false;
char data[HTTP_MIN_RECV];
int16_t ret = 0;
int16_t len = 0;

LOG_MESSAGE("Sending a HTTPS POST request to '%s'\r\n", HOSTNAME);

httpClientHandle = HTTPClient_create(&statusCode,0);
if(statusCode < 0){
    printError("httpTask: creation of https client handle failed", statusCode);
    continue;
}

ret = HTTPClient_setHeader(httpClientHandle,
                         HTTPClient_HFIELD_REQ_USER_AGENT,
                         USER_AGENT,strlen(USER_AGENT)+1,
                         HTTPClient_HFIELD_PERSISTENT);

ret = HTTPClient_setHeader(httpClientHandle, HTTPClient_HFIELD_REQ_CONTENT_TYPE,CONTENT_TYPE,strlen(CONTENT_TYPE),HTTPClient_HFIELD_PERSISTENT);
if(ret < 0){
    printError("httpTask: setting request header failed", ret);
    continue;
}


HTTPClient_extSecParams httpClientSecParams;
httpClientSecParams.rootCa = SSL_CA_CERT;
httpClientSecParams.clientCert = NULL;
httpClientSecParams.privateKey = NULL;


ret = HTTPClient_connect(httpClientHandle,HOSTNAME,&httpClientSecParams,0);
if(ret < 0){
    printError("httpTask: connect failed", ret);
    continue;
}
ret = HTTPClient_sendRequest(httpClientHandle,HTTP_METHOD_POST,REQUEST_URI,
                           post.msg,strlen(post.msg),
                           0);
if(ret < 0){
    printError("httpTask: send failed", ret);
    continue;
}

LOG_MESSAGE("HTTPS Response Code: %d\r\n", ret);

len = 0;
do
{
    ret = HTTPClient_readResponseBody(httpClientHandle, data, sizeof(data),
                                      &moreDataFlag);
    if(ret < 0){
        printError("httpTask: response body processing failed", ret);
        continue;
    }
    LOG_MESSAGE("%.*s \r\n", ret, data);
    len += ret;
}
while(moreDataFlag);

LOG_MESSAGE("Received %d bytes of payload\r\n", len);

ret = HTTPClient_disconnect(httpClientHandle);
if(ret < 0){
    printError("httpTask: disconnect failed", ret);
    continue;
}

HTTPClient_destroy(httpClientHandle);

  • The error is only occuring in my call of HTTPClient_sendRequest, and only begins to show up in the 16th post attempt and all other attempts after. A strange thing is happening when debugging where within that function, putting a breakpoint at the SlNetSock_close function call allows my data to send. As soon as I remove that breakpoint and let the program run on its own, the error returns.

  • Hi Brian,

    It's interesting that the problem seemingly goes away if you put a breakpoint at the SlNetSock_close() function. This is since while the CC3220 host MCU will be paused when you hit that breakpoint, the network processor (NWP) that actually handles the TCP/IP + Wi-Fi stack will still be free running, thus giving it time to perform tasks.

    Something useful to see what might be causing your problem are the NWP logs. Please follow the instructions here to mux out the NWP UART TX pin, and save the logs with a UART to PC adapter on that pin.

    http://processors.wiki.ti.com/index.php/CC3120_%26_CC3220_Capture_NWP_Logs

    Regards,

    Michael

  • Hi Michael,

    As per the instructions, do I have to use pin 62? Because I'm not using the launchpad, I do not have that pin broken out. I have been using pin 55 for UART0_TX, and my project has been based on the provisioning example project provided in the SDK. So whatever configurations were present in that project are still there.

  • So I just decided to add a vTaskDelay(10);  from the FreeRTOS task.c just before that SlNetSock_close(); is called in the HTTPClient_sendRequest from httpclient.c. This seems to have fixed my problem, though it's probably a bad way of doing so. So my HTTPClient_sendRequest function now looks as follows

    int16_t HTTPClient_sendRequest(HTTPClient_Handle client, const char *method,const char *requestURI, const char *body, uint32_t bodyLen, uint32_t flags)
    {
        HTTPClient_CB *cli = (HTTPClient_CB *)client;
        int16_t ret;
        uint8_t retryCounter = HTTPClient_MAX_REQUEST_ATTEMPTS;
    
        do
        {
            ret = sendRequest(cli,method,requestURI,body,bodyLen,flags);
            if (((ret <= SLNETERR_BSD_SOC_ERROR) && (ret >= SLNETERR_BSD_EALREADY)) || (ret == HTTPClient_ENOCONNECTION) || (ret == SLNETERR_ESEC_CLOSED))
            {
                /* In case the connection is not persistent or sudden disconnection occured, try to reconnect */
                vTaskDelay(10);
                SlNetSock_close(cli->ssock);
                setCliState(cli,CONNECTED_STATE,false);
                retryCounter--;
            }
            else if (ret == HTTP_SC_UNAUTHORIZED)
            {
                retryCounter--;
            }
        }while((((ret <= SLNETERR_BSD_SOC_ERROR) && (ret >= SLNETERR_BSD_EALREADY)) || (ret == SLNETERR_ESEC_CLOSED) || (ret == HTTP_SC_UNAUTHORIZED) || (ret == HTTPClient_ENOCONNECTION)) && (retryCounter > 0));
    
        /* Clears the non-persistent response filter */
        clearNonPersistResponseFilter(cli);
        /* Clears the non-persistent request headers */
        clearReqHeaders(cli,false);
    
        return (ret);
    }

  • Hi Brian,

    If you have fixed your issue by adding that small delay, then you should be able to keep doing so. 

    Were you getting the SLNETERR_BSD_ENSOCK error from sendRequest? Were you getting another error from that function? One possibility as to why adding that delay makes things work is that when you close the socket, it is actually not immediately closed. If you are using a TCP socket, then doing a close() will attempt to gracefully close it by sending a FIN packet. Thus, maybe the sockets were being kept open for a while after the close() calls.

    In any case, glad you figured out your issue. Let me know if you run into more issues with the HTTP client, or have further questions on this topic.

    Regards,

    Michael