Skip to content

Instantly share code, notes, and snippets.

@HudsonAfonso
Created November 13, 2018 14:38
Show Gist options
  • Save HudsonAfonso/e16d82af16b3c8204f6de190a4ebb5bc to your computer and use it in GitHub Desktop.
Save HudsonAfonso/e16d82af16b3c8204f6de190a4ebb5bc to your computer and use it in GitHub Desktop.
import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.net.*;
import java.util.Collections;
import java.util.Enumeration;
public class Companytec {
public static void main(String[] args) throws IOException {
System.setProperty("java.net.preferIPv4Stack", "true");
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
OUTER:
for (NetworkInterface interface_ : Collections.list(interfaces)) {
// we shouldn't care about loopback addresses
if (interface_.isLoopback())
continue;
// if you don't expect the interface to be up you can skip this
// though it would question the usability of the rest of the code
if (!interface_.isUp())
continue;
// iterate over the addresses associated with the interface
Enumeration<InetAddress> addresses = interface_.getInetAddresses();
for (InetAddress address : Collections.list(addresses)) {
// look only for ipv4 addresses
if (address instanceof Inet6Address)
continue;
// use a timeout big enough for your needs
if (!address.isReachable(3000))
continue;
System.out.format("ni: %s, ia: %s\n", interface_, address);
try {
InetAddress group = InetAddress.getByName("224.0.5.128");
MulticastSocket s = new MulticastSocket(62240);
s.setNetworkInterface(NetworkInterface.getByName(interface_.getName()));
s.joinGroup(group);
s.setSoTimeout(10000);
byte[] data = DatatypeConverter.parseBase64Binary("RElHSQABAAb///////8=");
s.send(new DatagramPacket(data, data.length, group, 2362));
int bufferSize = 1024 * 4;
// while (true) {
//Create buffer
byte[] buffer = new byte[bufferSize];
DatagramPacket dgram = new DatagramPacket(buffer, bufferSize, group, 2362);
s.receive(dgram);
String inetaddress = (dgram.getAddress()).getHostAddress();
String msg = new String(buffer);
s.close();
System.out.println(msg);
if (msg.indexOf("Companytec") > 0) {
System.out.println(inetaddress);
break OUTER;
}
} catch (Exception e) {
e.printStackTrace();
}
// }
// System.out.format("ni: %s, ia: %s\n", interface_, address);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment