Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Trusted Contributor
Posts: 117
Registered: ‎09-06-2011
My Device: 9900
My Carrier: myself
Accepted Solution

Find device on network

Could someone please explain how to find a device on a network - do I use multicasting or broadcasting?

 

I want to find the ip address of my server on wlan and there might be multiple instances of these servers. The server uses this class  http://www.blackberry.com/developers/docs/7.0.0api/net/rim/device/api/io/MulticastDatagramConnection... and is receiving repsonses ok. java then sends back string.

 

I only receive the following back on c++ side whereas I am hoping to receive a String of some kind (which im sending from java) So i can identify which responses are from my server. and which aren't.

 

M-SEARCH * HTTP/1.1
HOST:239.255.255.250:1900
ST:urn:schemas-upnp-org:device:InternetGatewayDevice:1
MAN:"ssdp:discover"
MX:3

 

 

And my code to send and receive is as follows..

 

int receiveMulti()
{
     struct sockaddr_in addr;
     int fd, nbytes,addrlen;
     struct ip_mreq mreq;
     char msgbuf[1024];

     u_int yes=1;            /*** MODIFICATION TO ORIGINAL */

     /* create what looks like an ordinary UDP socket */
     if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
	  perror("socket");
	  exit(1);
     }


/**** MODIFICATION TO ORIGINAL */
    /* allow multiple sockets to use the same PORT number */
    if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
       perror("Reusing ADDR failed");
       exit(1);
       }
/*** END OF MODIFICATION TO ORIGINAL */

     /* set up destination address */
     memset(&addr,0,sizeof(addr));
     addr.sin_family=AF_INET;
     addr.sin_addr.s_addr=htonl(INADDR_ANY); /* N.B.: differs from sender */
     addr.sin_port=htons(1900);

     /* bind to receive address */
     if (bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
	  perror("bind");
	  exit(1);
     }

     /* use setsockopt() to request that the kernel join a multicast group */
     mreq.imr_multiaddr.s_addr=inet_addr("239.255.255.250");
     mreq.imr_interface.s_addr=htonl(INADDR_ANY);
     if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
	  perror("setsockopt");
	  exit(1);
     }
     printf("beforeread");
     fflush(stdout);
     bool keepGoing = true;
     int count = 0;
     /* now just enter a read-print loop */
     while (keepGoing) {
    	 count++;
	  addrlen=sizeof(addr);
	  if ((nbytes=recvfrom(fd,msgbuf,1024,0,
			       (struct sockaddr *) &addr,(socklen_t*)&addrlen)) < 0) {
	       perror("recvfrom");
	       exit(1);
	  }
	  msgbuf[nbytes] = '\0';
	  printf("received: %d  %s", nbytes, msgbuf);
	  fflush(stdout);
	  if(count > 5)
	  {
		  break;
	  }
     }
}


int sendMulti ()
{
     struct sockaddr_in addr;
     int fd, cnt;
     struct ip_mreq mreq;
     char *message="83383329092240912";

     /* create what looks like an ordinary UDP socket */
     if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
	  perror("socket");
	  exit(1);
     }

     /* set up destination address */
     memset(&addr,0,sizeof(addr));
     addr.sin_family=AF_INET;
     addr.sin_addr.s_addr=inet_addr("239.255.255.250");
     addr.sin_port=htons(1900);

     /* now just sendto() our destination! */
    // while (1)
     {
	  if (sendto(fd,message,sizeof(message),0,(struct sockaddr *) &addr,
		     sizeof(addr)) < 0) {
	       perror("sendto");
	       exit(1);
	  }
	  sleep(1);
     }
}

 

 

tjhanks.

Trusted Contributor
Posts: 117
Registered: ‎09-06-2011
My Device: 9900
My Carrier: myself

Re: Find device on network

I might be approaching this totally wrong as I am just learning about this from the web so if I am please someone put me right!

 

I did try a different ip address (which was still multicast compliant) and port but I never received a response whatsoever but now I'm realizing the c++ code is receiving data when my java server is inactive so I think now maybe I can never receive anything java to c++ and thats the problem. The received stuff is from other multicast software on the network.

 

stumped.

Trusted Contributor
Posts: 117
Registered: ‎09-06-2011
My Device: 9900
My Carrier: myself

Re: Find device on network

It turns out my assumption the sample code I was using sends back to the group with this line of code is incorrect..

 

String remoteAddr = dgram.getAddress();
   String msg = "Hi";

   dgram = conn.newDatagram( msg.getBytes(), msg.length(), remoteAddr);

 

remoteAddr is giving me back //192.168.0.5:65228;4321|UDP which I was using to send the data back to c++ but when I just changed the address to my group and port manaually the packets are received c++ side.