Java multicast not working

I am developing one peer-to-peer application for my class, and I was told that letting the servers detect each other in Multicast on their UDP port 1110 and listen on their UDP port 1110. I wrote the code as shown below for doing this. for testing, I run 2 servers that send and receive. but nothing seems to work. where do you think my problem is?

I put 2 servers in 2 different folders. and I assigned the IP addresses to my network card as follows ifconfig eth0:3 192.168.0.11 netmask 255.255.255.0 up , how should I inform each server about a new ip address?

Broadcastlister

 class BroadcastListner implements Callable<Object> { int PORT = 1110; String IP = "255.255.255.255"; MulticastSocket socket ; DatagramPacket packet; InetAddress IPAD; byte data[] = null ; //////////////change size int numOfNodes; BroadcastListner(String IP, int numOfNodes) { try { this.numOfNodes = numOfNodes; this.IP = IP; IPAD = InetAddress.getByName(IP); socket = new MulticastSocket(PORT); packet = new DatagramPacket(data,data.length); } catch (Exception e) { e.printStackTrace(); } } BroadcastListner(int numOfNodes) { try{ this.numOfNodes = numOfNodes; // this.IP = IP; IPAD = InetAddress.getByName(IP); socket = new MulticastSocket(PORT); packet = new DatagramPacket(data,data.length); } catch (Exception e) { e.printStackTrace(); } } public String call() { try{ socket.joinGroup(IPAD); } catch (Exception e) { e.printStackTrace(); return ""; } while(true) { try { socket.receive(packet); String str = new String(packet.getData()); System.out.println(" Time signal received from"+ packet.getAddress() + " Time is : " +str); } catch (Exception e) { e.printStackTrace(); return ""; } } //socket.leaveGroup(IPAD); //socket.close(); //return ""; } } 

BroadcastSender

 class BroadcastSender implements Callable<Object> { int PORT = 1110; String IP = "255.255.255.255"; MulticastSocket socket; DatagramPacket packet; InetAddress IPAD; byte[] data = "IAmAServer".getBytes(); //int numOfNodes; String str = "IAmAServer"; BroadcastSender(String IP) { try { // this.numOfNodes = numOfNodes; this.IP = IP; IPAD = InetAddress.getByName(IP); socket = new MulticastSocket(); } catch (Exception e) { e.printStackTrace(); } } BroadcastSender() { try{ // this.numOfNodes = numOfNodes; // this.IP = IP; IPAD = InetAddress.getByName(IP); socket = new MulticastSocket(); } catch (Exception e) { e.printStackTrace(); } } public String call() { try { socket.joinGroup(IPAD); socket.setTimeToLive(10); } catch (Exception e) { e.printStackTrace(); return ""; } while(true) { try { Thread.sleep(2000); packet = new DatagramPacket (data,str.length(),IPAD,PORT); socket.send(packet); } catch (Exception e) { e.printStackTrace(); return ""; } } //return ""; } } 
+4
source share
1 answer

You need to try the broadcast address 192.168.0.255

An alternative is to use multi-broadcast instead of a broadcast address, such as 224.xxx, which is not tied to a specific subnet.

0
source

Source: https://habr.com/ru/post/1394880/


All Articles