setsockopt OPT_RECEIVE_NOWAIT and OPT_RECEIVE_TIMEOUT Does not Work

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

setsockopt OPT_RECEIVE_NOWAIT and OPT_RECEIVE_TIMEOUT Does not Work

Jump to solution
1,992 Views
mohwaqas12
Contributor III

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
Labels (1)
Tags (1)
0 Kudos
1 Solution
661 Views
Mohsin455
Contributor IV

Yes,

 

         You will need to use uint_32. I also faced similar issues as I was using uint_8 to pass the parameter value but the function does not take the sizeof() parameter into account.

 

Regards,

Mohsin

View solution in original post

0 Kudos
2 Replies
661 Views
Tgomes
Contributor III

Hi mohwaqas12,


Try changing the type of "value" of int_32 to uint_32. I know it seems pointless, but already had problems similar to yours. In my project, I have a call almost identical to yours, except I'm using an unsigned int, and that usually works.

Hope this helps.
Good luck

662 Views
Mohsin455
Contributor IV

Yes,

 

         You will need to use uint_32. I also faced similar issues as I was using uint_8 to pass the parameter value but the function does not take the sizeof() parameter into account.

 

Regards,

Mohsin

0 Kudos