networking - Java IP Multicast issue when sender and receiver on different node -


hi trying out java multicast.

i have wifi router @ - 10.0.0.1 (gateway)

and 2 nodes:

node_1 - 10.0.0.4 node_2 - 10.0.0.3

my ip multicast sender looks like:

private static class sender extends thread {     // create socket don't bind going send data     private multicastsocket s;     private static int senderport = 15000;     private static string group = "225.4.5.6";      public sender() throws ioexception     {         s = new multicastsocket(senderport);         s.setinterface(inetaddress.getlocalhost());         s.joingroup(inetaddress.getbyname(group));     }      @override     public void run() {         integer data = 1;          while(true)         {             try {                 s.send(new datagrampacket(data.tostring().getbytes(), data.tostring().getbytes().length, inetaddress.getbyname(group), senderport));                 thread.sleep(3000);                 data++;             } catch (unknownhostexception e) {                 // todo auto-generated catch block                 system.out.println("sender - unknownhostexception");             }catch (ioexception e) {                 // todo auto-generated catch block                 system.out.println("sender - ioexception");             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }          }     } } 

and ip multicast receiver looks like:

private static class receiver extends thread {     private multicastsocket s;     private static int receiverport = 15000;     private static string group = "225.4.5.6";      public receiver() throws ioexception     {         s = new multicastsocket(receiverport);         s.setinterface(inetaddress.getlocalhost());         s.joingroup(inetaddress.getbyname(group));      }      @override     public void run() {          while (true)         {             byte buf[] = new byte[1024];             datagrampacket pack = new datagrampacket(buf, buf.length);             try {                 system.out.println("receiver waiting data");                 s.receive(pack);             } catch (ioexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }              system.out.write(pack.getdata(),0,pack.getlength());             system.out.println();         }     } } 

when have both sender , receiver in same node works when have them in different nodes not work.

what missing here??

by calling setinterface() local host preventing joingroup() message leaving current host. doesn't matter in sender, because sender doesn't have join group anyway, in receiver prevent other hosts, routers, etc. knowing receiving host in group.

just remove it.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -