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.

CC3000 select () does not detect incoming data



Hello Community,

I want to accept one client to connect to my server application, but when I use

  ret = select(nfds+1, pR, NULL, pE, (struct timeval *)timeout);

no reading is reported. I know that the client want to connect to my server! Only an exeption will occure after 60 sec. (But this is controlled by netapp_timeout_values())

I open a socket, configure it (setsockopt(socket, SOL_SOCKET, SOCKOPT_NONBLOCK, &nonBlocking, sizeof(nonBlocking)); ), bind it, listen on one connection and poll select. After polling I want to accept the connection, because I want no blocking at accept.

How did I use/configure the select methode to detect any client connection to my socket?

Regards,

Dennis

  • I solved my problem! I have at first accept any client and than select works well.

    Dennis

  • I have the same problem with you, but I I can not understand your solution, can you give me your source code! Thanks a lot!!!

  • Hello yong,

    my problem was that I have used select on the socket of the server and not on the socket of any connected client!

    This was my test source it works with TI Satelite demo. Maybe some names of the function and definitions not correct. (e.g. SOCK_*, but the functions and definitions only wrapper for TI's library)

    void CC3000_FrameSensorEmulator_v1 ()
    {
      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      // init
    
      while (1)
      {
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // socket functions
        debug_printf("socket\n");  
    
        int retval = 0;
    
        SOCK_SOCKET server_socket = SOCK_socket( SOCK_AF_INET, SOCK_SOCK_STREAM, SOCK_IPPROTO_TCP );
    
        if ( 0 > server_socket) CC3000_ERROR_AND_STOP ("\nerror SOCK_socket: %d\n", server_socket);
    
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // bind
        debug_printf("bind\n");  
    
        SOCK_sockaddr_in server_address;
        int port = SERVER_PORT;
    
        memset(&server_address, 0x00, sizeof (SOCK_sockaddr_in));
    
        server_address.sin_family = SOCK_AF_INET;
    
        // Set the Port Number
        server_address.sin_port = SOCK_htons(port);
    
        // Set ip 0.0.0.0
        server_address.sin_addr.S_un.S_addr = SOCK_htonl(INADDR_ANY);
    
        retval = SOCK_bind( server_socket, (SOCK_sockaddr*)&server_address, sizeof(SOCK_sockaddr_in) );
    
        if ( 0 != retval) CC3000_ERROR_AND_STOP ("\nerror SOCK_bind: %d\n", retval);
    
    
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // listen
        retval = SOCK_listen( server_socket, 1 );
    
        if ( 0 != retval) CC3000_ERROR_AND_STOP ("\nerror SOCK_listen: %d\n", retval);
    
           
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // ioclt
        debug_printf("ioclt\n");  
    
        short  nonBlocking = 0;
        unsigned long  recvtimeout = 100000; //100ms
        retval = setsockopt(server_socket, SOL_SOCKET, SOCKOPT_NONBLOCK     , &nonBlocking, sizeof(nonBlocking));
        retval |= setsockopt(server_socket, SOL_SOCKET, SOCKOPT_RECV_TIMEOUT , &recvtimeout, sizeof(recvtimeout));
    
        if ( 0 > retval) CC3000_ERROR_AND_STOP ("\nerror setsockopt: %d\n", retval);
    	
    
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // accept
        debug_printf("accept\n");  
    
        BOOL isaccept = FALSE;
    
        SOCK_SOCKET client_socket;
        SOCK_sockaddr_in client_address;
        int sin_size = 0;
    
        while (!isaccept)
        {
          CC3000_Sleep_MicroSeconds( 100000 ); //sleep 100ms
    
          sin_size = sizeof(SOCK_sockaddr_in);
          memset(&client_address, 0x00, sin_size);
    
          client_socket = SOCK_accept( server_socket, (SOCK_sockaddr*)&client_address, &sin_size );
    
          if ( SOC_ERROR == client_socket ) 
          {
            SOCK_close(server_socket);
            CC3000_ERROR_AND_STOP ("\nerror SOCK_accept: %d\n", client_socket);
          } 
          else if ( SOC_IN_PROGRESS == client_socket )
          {
            debug_printf("connection pending: %d\n", client_socket);
          } 
          else if ( 0 <= client_socket )
          {
            isaccept = TRUE;
          } 
          else
          {
            isaccept = FALSE;
          }
        }
    
        hci_unsolicited_event_handler();
    
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // select
        debug_printf("select\n");
    
        BOOL isselct = FALSE;
    
        SOCK_fd_set readfd;
        //SOCK_fd_set writefds;
        //SOCK_fd_set exceptfds;
    
        struct SOCK_timeval timeout;
    
        while (!isselct)
        {
          SOCK_FD_ZERO ( &readfd );
          SOCK_FD_SET  ( client_socket, &readfd); 
    
          timeout.tv_sec = 15;
          timeout.tv_usec = 0;
    
          retval = SOCK_select( client_socket, &readfd, NULL, NULL, &timeout ); 
    
          if ( (retval > 0) && (SOCK_FD_ISSET(client_socket, &readfd)) ) 
          {
            isselct = TRUE;
          } 
          else 
          {
            isselct = FALSE;
            CC3000_Sleep_MicroSeconds( 100000 ); //sleep 100ms
          }
    
        }
    
        hci_unsolicited_event_handler();
    
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // receive
        debug_printf("receive\n");   
    
        char buffer[6];
        int readcnt = 0;
    
        do
        {
          int sin_size = sizeof (SOCK_sockaddr_in);
          memset(buffer, 0x00, sizeof(buffer));
    
          //readcnt = SOCK_recvfrom( client_socket, buffer, sizeof(buffer), 0, (SOCK_sockaddr*)&client_address, &sin_size );
          readcnt = SOCK_recv( client_socket, buffer, sizeof(buffer), 0);
    
          CC3000_Sleep_MicroSeconds( 100000 ); //sleep 100ms
        } while (0 >= readcnt);
    
        lcd_printf  ("SOCK_recv: %s\n", buffer);
        debug_printf("SOCK_recv: %s\n", buffer);
        
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // send   
        debug_printf("send\n");  
    
        while (1)
        {
          unsigned char dataPacket[] = { '\r', 0xBE, 128, 128, 128, 60, 24, 0xEF };
    
          SOCK_send( client_socket, (const char*)dataPacket, sizeof(dataPacket), 0);
    
          CC3000_Sleep_MicroSeconds( 200000 ); //sleep 200ms
        }
    
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        //close
        debug_printf("close\n");  
    
        SOCK_close(server_socket);
        SOCK_close(client_socket);
      }
    }

    I hope it helps and it is not to confusing.

    Dennis

  • Hello Dennis

        Thanks for your kindly help!

        I wanted to use select together with accept(), just recently, i learned cc3000 not support it. Your solution is helpful for me! Thank you!!!

    Yong