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.

[ CC3200 connection timeout ] non-block socket / sl_select

Other Parts Discussed in Thread: CC3200, ENERGIA

hi .

i want to sl_connect() with timeout. (sl_connect default timeout is 10sec.)

but sl_connect function is not support timeout parameter.

so. i use  NonBlocking socket / sl_select for sl_connect timeout.

but, it is not working.

How I use connection timeout ?

< this is my test sequence & code >

i test at  tcp_socket  example.

i use simple python server ( bind -> accept -> read socket)

this is connection check sequence.

> create socket.

> change socket to non-blocking

> sl_connect()  call

> if sl_connect() return value  is SL_SOC_OK, then connection success

> if sl_connect() return value is SL_EALREADY, then connecting.

      > sl_select -  check write fd with 3sec. timeout

      >  if  sl_select return : 0 --> 3sec. timeout (connection fail)

      > if  sl_select return < 0 --> select function error (connection fail)

      > if sl_select return 1 or > 0 --> check socket error code.

                                                         --> i want to check GetSockOpt with SL_SOC_ERROR parameter

                                                              but, GetSockOpt not support SL_SOC_ERROR.

 

                                                             so, i check with  SL_FD_ISSET(iSockID, &writeFd)

                                                              > it is passed, but sl_send function return -1;

> change socket to blocking

-------------------------------------------------------------------------------------------------------------------------------------

i modify BsdTcpClient() function like this.

int BsdTcpClient(unsinged char usPort)

{

 ......

#if 0

     // this is original code. // blocking sl_connection
     // connecting to TCP server
     iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);


#else

//this is my code,   non-blocking connection
// non blocking setting
long nonBlocking = 1;
int iSetOptStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlocking, sizeof(nonBlocking));

// connect
iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);

if (iStatus == SL_SOC_OK)
{
    Report("sl_connected\r\n");
}
else
{
    // connecting
    if (iStatus == SL_EALREADY)
    {
        SlTimeval_t timeout;
        SlFdSet_t WriteFds;

        timeout.tv_sec = 3; // sec.
        timeout.tv_usec= 0; // msec.

        SL_FD_ZERO(&WriteFds);
        SL_FD_SET(iSockID, &WriteFds);

        // select
        int result = sl_Select(iSockID + 1, NULL, &WriteFds, NULL, &timeout);

        if (result > 0)
        {
            int so_error = FAILURE;
            socklen_t len = sizeof(so_error);

            // socket error check
#if 0
            //connection check case#1 : sl_GetSockOpt does not support SL_SOC_ERROR Parameter.
            sl_GetSockOpt(iSockID, SL_SOL_SOCKET, SL_SOC_ERROR, &so_error, &len);
            Report("so_error : %d\r\n", so_error);
#else
            //connection check case#2 : just file description setting check
            //                                                 but in this case,  sl_send function error (-1)
           if (SL_FD_ISSET(iSockID, &WriteFds))

           {
               so_error = SL_SOC_OK;
           }
#endif
          if (so_error == SL_SOC_OK)
          {
              Report("connection success\r\n");
             iStatus = SUCCESS;
          }
         else
        {
             Report("connection fail or pending\r\n");
       }
}
else if (result == 0)
{
       Report("connection timeout \r\n");
}
else
{
        Report("connection error (code : %d)\r\n", result);
}

#endif

    sl_send()       // send data to server 

...

  • Hi Kim,

    Please changes code to:

    long nonBlocking = 1;
        int iSetOptStatus;
        iSetOptStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlocking, sizeof(nonBlocking));
    
        // connecting to TCP server
        iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
        if( iStatus < 0 )
        {
            // error
          if (iStatus != SL_EALREADY)
          {
            sl_Close(iSockID);       
            ASSERT_ON_ERROR(CONNECT_ERROR);
          }
          
           SlTimeval_t timeout;
           SlFdSet_t WriteFds;
           
           SL_FD_ZERO(&WriteFds);
           SL_FD_SET(iSockID, &WriteFds);
           
            int result = sl_Select(iSockID + 1, NULL, &WriteFds, NULL, &timeout);
    
            if (result > 0)
            {
                int so_error = FAILURE;
                socklen_t len = sizeof(so_error);
                if (SL_FD_ISSET(iSockID, &WriteFds))
               {
                   iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
                   if(iStatus < 0)
                   {
                     
                     so_error = SL_SOC_OK;
                   }
                   else
                   {
                     so_error = -1;
                   }
               }
               if (so_error == SL_SOC_OK)
               {
                  Report("connection success\r\n");
                  iStatus = SUCCESS;
               }
               else
               {
                 Report("connection fail or pending\r\n");
               }
            }
            else if(result == 0)
            {
              Report("connection timeout \r\n");
              sl_Close(iSockID);       
              ASSERT_ON_ERROR(CONNECT_ERROR);
            }
            else
            {
              Report("connection error (code : %d)\r\n", result);
              sl_Close(iSockID);       
              ASSERT_ON_ERROR(CONNECT_ERROR);
            }
        }
    sl_send();


    Regards,

    Aashish

  • Hi Kim,


    We are closing this thread, for follow up queries please open a new thread and add a link to this one for reference.


    Regards,
    Aashish
  • I am facing the same issue.

    My TCP client on cc3200 hangs for 30 seconds if no internet is there.

    Kindly provide some help.I am using Energia as the IDE and I saw that in WifiClient.cpp file already my flow is non blocking.

    sl_SetSockOpt(socketHandle, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &enableOption, sizeof(enableOption));

    Now what changes I should make in cpp file. Should I follow the above snippet that you wrote.

    And in which part of code should I include it.

    Thanks,

    Agam.

  • Hi Agam,

    This is a old thread and already closed. Please for new question always open new thread. Thank you.

    Jan