AnsweredAssumed Answered

setsockopt OPT_RECEIVE_NOWAIT and OPT_RECEIVE_TIMEOUT Does not Work

Question asked by Muhammad Waqas Athar on May 21, 2012
Latest reply on May 21, 2012 by Mohsin455

Hello ALL., 

I am trying to set  OPT_RECEIVE_NOWAIT  and OPT_RECEIVE_TIMEOUT using setsockopt but nothing seems to work.It does not give any error but also does not work as expected.What i want to achieve is to read one byte over tcp socket without PUSH flag being set at remote endpoint.As per documentation i found this to be most appropriate for my case. Below is my socket task in which i was testing this setsockopt functionality

 

void socket_task(uint_32 data)
 {
   printf("SOCKET TASK \r\n");
    sockaddr_in laddr,raddr;
   int_32 sock,listensock;
   int_32 length=0;
   int_32 error;
   int_32 value;
   int_32 socklist[3];
   uint_32 i=0;
   uint_16 rlen;
   char buffer[200];
   memset(buffer,0,200);
     laddr.sin_family=AF_INET;
   laddr.sin_port=1234;
   laddr.sin_addr.s_addr = INADDR_ANY;
      sock = socket(PF_INET,SOCK_STREAM,0);
   if(sock==RTCS_SOCKET_ERROR){
     printf("Failed to create TCP Socket \r\n");
     _task_block();
    }
    value=1000;
       error = setsockopt(sock,SOL_TCP,OPT_RECEIVE_TIMEOUT,&value,sizeof(value));
                      if (error!=RTCS_OK){
                       printf("setsockopt receive timeout failed \r\n");
                     }
   value=TRUE;
   error = setsockopt(sock,SOL_TCP,OPT_RECEIVE_NOWAIT,&value,sizeof(value));
     if (error!=RTCS_OK){
         printf("setsockopt receive nowait failed \r\n");
         }      
   error=bind(sock,&laddr,sizeof(laddr));
   if(error!=RTCS_OK){
   printf("Failed to bind socket \r\n");
   _task_block();
   }
   error = listen(sock,0);
   if(error!=RTCS_OK){
   printf("Failed to bind socket \r\n");
   _task_block();
   }
      listensock=sock;
   socklist[0]= listensock;
   i++;
   while(1){
   sock = RTCS_selectset(socklist,i,0); // i had to use this as i had unconnected socket in rtcs that was giving problem with selectall 
     //sock=RTCS_selectall(0); 
     if (sock == RTCS_SOCKET_ERROR){
       printf("Error RTCS_selectall() failed\r\n");
       _task_block();
     }else if (sock == listensock){
       rlen = sizeof(raddr);
       sock  = accept(listensock,&raddr,&rlen);
       value=1000;
       error = setsockopt(sock,SOL_TCP,OPT_RECEIVE_TIMEOUT,&value,sizeof(value));
                      if (error!=RTCS_OK){
                       printf("setsockopt receive timeout failed \r\n");
               }
        value=TRUE;
        error = setsockopt(sock,SOL_TCP,OPT_RECEIVE_NOWAIT,&value,sizeof(value));
           if (error!=RTCS_OK){
             printf("setsockopt receive nowait failed \r\n");
               }            
           value=0;
           error = getsockopt(sock,SOL_TCP,OPT_RECEIVE_TIMEOUT,&value,(uint_32*)&length);
           if(error!=RTCS_OK){
              printf("getsockopt receive timeout failed \r\n");
           }
           printf("Getsockopt receive timeout : %d",value);
           // Read the socket options
        socklist[1] = sock;
       i =2;
       if (sock==RTCS_SOCKET_ERROR){
        printf("Accept failed error 0x%1x",RTCS_geterror(listensock)); 
        continue;
       }
     } else{          
     length=recv(sock,buffer,200,0);
      if (length==RTCS_ERROR){
         error =RTCS_geterror(sock); 
         if (error!=RTCS_OK){
         printf("Error 0x%x recieving from %d.%d.%d.%d\r\n",error,
                (raddr.sin_addr.s_addr>>24)&0xFF ,
                (raddr.sin_addr.s_addr>>16)&0xFF ,
                (raddr.sin_addr.s_addr>>8)&0xFF ,
                (raddr.sin_addr.s_addr)&0xFF );
        shutdown(sock,FLAG_ABORT_CONNECTION);
         }else{
          printf("Recieved : %s \r\n",buffer); 
         memset(buffer,0,200); 
         }
        }
       else
        printf("Recieved : %s \r\n",buffer); 
         memset(buffer,0,200);
    }
   } 
 }
Does anyone know how to make OPT_RECEIVE_TIMEOUT & OPT_RECEIVE_NOWAIT  setsockopt working with some example.I am using mqx 3.7 with Kinetics K60 Tower Board. 
Thanks

Outcomes