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.

Ask question,my NDK server on DM642 can't receive broadcast message from PC ......

Hello everyone,

I  try to receive broadcast message from client on PC to DM642,but it doesn't work

who can help me ?

DM642 is server ,to receive message.while PC is client,to send broadcast message

the IP on DM642 is 192.168.1.77,mask is 255.255.255.0,Gateway IP is 192.168.1.1,the port  is 7000.

the PC  calls to 192.168.1.255,port is 7000.

there is my piece of code:

void tskServer()

{

	int broadsock;
struct sockaddr_in broadClient,broadaddr;
int len = sizeof(struct sockaddr_in);
char buf[100];
int so_broadcast=1;
	struct timeval timeout={10,0};	
   	fdOpenSession(TaskSelf()); 

bzero( &broadaddr, sizeof(struct sockaddr_in) );
broadaddr.sin_addr.s_addr = inet_addr("192.168.1.77");
broadaddr.sin_family = AF_INET;
broadaddr.sin_port = htons(7000);
broadaddr.sin_len = sizeof(broadaddr);
        broadsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
     setsockopt(broadsock,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout));     
         setsockopt(broadsock,SOL_SOCKET,SO_BROADCAST,&so_broadcast,sizeof(int));


 setsockopt(broadsock,SOL_SOCKET,SO_IFDEVICE,(void *)&so_broadcast,sizeof(int));
 bind(broadsock,&broadaddr,len);
       recvfrom(broadsock,(void *)buf,100,MSG_WAITALL,(PSA)&broadClient,&len);
	fdCloseSession(TaskSelf());
TaskBlock(TaskSelf());
 exit(1);
}

  • I had a similar issue with the C6472 DSP.  Depending on which NDK you are using, see this post:

    http://e2e.ti.com/support/dsp/c6000_multi-core_dsps/f/439/t/122182.aspx?PageIndex=3

    If that link doesn't work, the post is titled "NDK RX Filter".  I marked the fix as the verified answer.

    Nick

  • Nick:

            Thanks for your reply,but I don't catch your point.

            Is there anything wrong with my NDK configuration ?

             I  tried  to  bind   NULL  ip  address(INADDR_ANY)  with  the  socket,   it   seems  better,  

             but   I   don't   know why   it   can   receive   message with the socket   that  has no  ip address?

  • Is your device able to receive directed UDP packets to IP 192.168.1.77 but not broadcast packets sent to 255.255.255.255?  If so, then the problem might be the same as mine.  I had to recompile the NDK stack.lib with the change mentioned in the referenced post, then rebuild my project.  That seemed to fix receiving broadcast packets for me, as long as they were sent to the correct port number that the device was listening on. 

    I have been using a similar NDK configuration to yours.  One difference is my bind() is in a while-loop.  Also, your recvfrom() call should probably be in a loop.  I believe it will timeout in 10 seconds even though you are using MSG_WAITALL since you set the SO_RCVTIMEO option.  My configuration is below as a reference:

       SOCKET          s = INVALID_SOCKET;
       struct sockaddr_in              sin;
       struct timeval                  timeout;     
       char                                 *pBuf;
       HANDLE                          hBuffer;

       int           tmp;
       int           cnt;
       int            val;
       Int            status;

        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        
       // setup for broadcast transmits
       val = 1;
       tmp = setsockopt( s, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
       tmp = setsockopt( s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));  
       tmp = setsockopt( s, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));

       // initialize the addresses we are receiving at
       bzero(&sin, sizeof(struct sockaddr_in));
       sin.sin_family       = AF_INET;
       sin.sin_len          = sizeof(sin);
       sin.sin_addr.s_addr  = inet_addr("192.168.1.77");
       sin.sin_port         = htons(7000);
      
       // bind to known address/port
       while ( bind(s, (PSA)&sin, sizeof(sin)) < 0);
      
       // Configure our socket timeout to be 10 ms
       timeout.tv_sec  = 0;
       timeout.tv_usec = 10000;
       tmp = setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof( timeout ) );
       tmp = setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof( timeout ) );

      while(1)
       {
       
        /* ----------------------------------------------------------------------
        * Receive message over ethernet
       * ---------------------------------------------------------------------*/   
        cnt = 0;
        tmp = sizeof(sin);
        cnt = recvncfrom(s, (void**)&pBuf, MSG_WAITALL,(PSA)&sin, &tmp,&hBuffer); //receive with no copy    
        if (cnt > 0)
        {   

            // Do stuff with message 

        } // end if

    }// end while(1)