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.

Problems with Select() and setsockopt()



I am trying to get the select() function to work so it will check if any packages has arrived so that I know when to activate recvfrom() function. As it is now recvfrom() blocks the process for 60 sec and this will not work for my application.

setsockopt() is a function I am trying to use to put the system in a nonblocking mode, but I am not sure if I have made the right commands for it. Does it look correct?

select() should be active 5k ms to check if there are any messages, but sel0 is always zero! Have I mixed up any commands or misunderstood the usage of the function? 

I would be grateful for any help in this matter! 

// The family
addrServer. sa_family = AF_INET;

// The Server port:
addrServer. sa_data[0] = Port[0];
addrServer. sa_data[1] = Port[1];

// The Server IP address: 
addrServer. sa_data[2] = IP_Server[0];
addrServer. sa_data[3] = IP_Server[1];
addrServer. sa_data[4] = IP_Server[2];
addrServer. sa_data[5] = IP_Server[3];

iReturnSOCKETServer = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

struct timeval timeout;
timeout. tv_sec = 0;
timeout. tv_usec = 5000;
 fd_set fdSet;
FD_ZERO(&fdSet);
FD_SET(iReturnSOCKETServer, &fdSet);

memset (&addrServer.sa_data [2], 0, 4);

iReturnBINDServer = bind(iReturnSOCKETServer, &addrServer, sizeof (sockaddr ));

int nonBlocking = SOCK_ON;
setsockopt(iReturnSOCKETServer, SOL_SOCKET, SOCKOPT_NONBLOCK, &nonBlocking, sizeof (nonBlocking));

sel0 = select(iReturnSOCKETServer+1, &fdSet, NULL, NULL, &timeout);     // Always zero
UARTprintf ("Select: %i\n" , sel0);
sel1 = listen(iReturnSOCKETServer, 0);     // Always zero
UARTprintf ("listen iReturnSOCKETServer: %i\n" , sel1);

if (!FD_ISSET(iReturnSOCKETServer,&fdSet))
{
     UARTprintf ("FD is not set!\n" );     // This is always printed!
}
iReturnREQUESTServer = recvfrom(iReturnSOCKETServer, ucRx_Buffer, CC3000_APP_BUFFER_SIZE, 0, &addrServer, &tRxPacketLength);
UARTprintf ("Command: %c\n" ,ucRx_Buffer[0]);
  • Hi Martin,

    If you use the newest patch, non blocking mode is enabled by default. So you don't need setsocketopt() for that.

    Try calling listen() befor select().

    regards

    Florian

  • I am using the following software:
    Release Pack ver: 1.11.1
    System Pack ver: 5.14.7.13
    Service Pack ver: 1.24
    Thanks Florian, but according to the API listen() can only be used with sock_stream and that in turn is used with TCP and not UDP that I am using.
    I have it kind of working if I put it in a separate function call, my main problem now is that I want to use a timer that will check or send information every second or so but none of the functions related to the CC3000 API works when they are used in a timer function. Does anyone here have any idee to why there is a direct problem when using timers? The system always ends up spi.c line 651, the following code:
    // Due to the fact that we are currently implementing a blocking situation
    // here we will wait till end of transaction
    while (eSPI_STATE_IDLE != sSpiInformation. ulSpiState )
    {
     ;
    }
    return(0);

    Here is my code for setting up the timer:
    // Enable processor interrupts.
    IntMasterEnable ();

    // Configure 32-bit periodic timers.
    TimerConfigure (TIMER0_BASE, TIMER_CFG_PERIODIC);
    TimerLoadSet (TIMER0_BASE, TIMER_A, SysCtlClockGet ());

    // Setup the interrupts for the timer timeouts.
    IntEnable (INT_TIMER0A);
    TimerIntEnable (TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    ROM_TimerLoadSet( TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet()*2);
    TimerEnable (TIMER0_BASE, TIMER_A);

    This is the timer function:
    void Timer0IntHandler( void )
    {
            // Clear the timer interrupt.
           TimerIntClear (TIMER0_BASE, TIMER_TIMA_TIMEOUT);
           pcData = "C" ;
           ulDataLength = 1;
           sendto(Client_Socket, pcData, ulDataLength, 0, &addrClient, sizeof (sockaddr ));
    }
  • Hi Martin,

    I have not tried APIs directly in timer/interrupt context. Since most of the APIs are blocking in your case, I'm not sure if the platform allows you to do that. Can you please try with 'SOCKOPT_RECV_NONBLOCK' flag set to '1' in setsockopt?

    Thanks & Regards,

    Raghavendra