Reputation: 51
I'm trying to resolve the IP Address of a BeagleBone Black device which is on the same network of the Android device running my app. The hostname of my device is "tovremgw.local"
. If I try to ping from my windows pc commandline it will successfully return the IPv6 address.
When using the following code:
new Thread(new Runnable(){
public void run(){
Inet6Address addr = null;
try {
addr = (Inet6Address) Inet6Address.getByName("tovremgw.local");
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println(addr);
}
}).start();
It will return Exception :
"java.net.UnknownHostException: Unable to resolve host "tovremgw.local": No address associated with hostname"
I'm running the app in an emulator on the same pc i'm pinging the BeagleBone with.
Upvotes: 0
Views: 489
Reputation: 51
I don't know if it's the best answer, but i did not manage to get it working using other methods. I don't know if this helps anyone who has the same problem, but i will post my code just to illustrate how i did it.
public String initUdpClient() throws IOException, InterruptedException {
String tovremgwip = "";
DatagramSocket s = new DatagramSocket();
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
int server_port = 5353;
InetAddress inetaddress = InetAddress.getByName("224.0.0.251");
char[] bytearray = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x74, 0x6f, 0x76,
0x72, 0x65, 0x6d, 0x67, 0x77, 0x05, 0x6c, 0x6f,
0x63, 0x61, 0x6c, 0x00, 0x00, 0x01, 0x00, 0x01
};
String messageStr = new String(bytearray);
int msg_length = messageStr.length();
byte[] message = messageStr.getBytes();
DatagramPacket p = new DatagramPacket(message, msg_length, inetaddress, server_port);
for(int i = 0; i < 3; i++){
s.send(p);
s.receive(packet);
int length = packet.getLength();
if(length > 0){
int[] temparr = new int[length];
for(int y = length - 4; y < length; y++){
tovremgwip = tovremgwip + Integer.toString(packet.getData()[y] & 0xFF);
if(y != length - 1) tovremgwip = tovremgwip + ".";
}
return tovremgwip;
}
packet.setLength(buffer.length);
TimeUnit.SECONDS.sleep(1);
}
return "";
}
I managed to copy the packet sent by my PC using when pinging the Beaglebone using wireshark. Then get the last four bytes of the message received.
Upvotes: 1
Reputation: 1888
As per BeagleBoard documentation
If your Beagle is connected to your local area network (LAN) via either Ethernet or WiFi, it will utilize mDNS to broadcast itself to your computer. If your computer supports mDNS, you should see your Beagle as beaglebone.local. Non-BeagleBone boards will utilize alternate names. Multiple BeagleBone boards on the same network will add a suffix such as beaglebone-2.local.
The naming service protocol used here is Multicast DNS which is different from standard DNS protocol (Unicast). Since Inet6Address supports only standard DNS and that too records of type 'A' or 'AAAA', it will not serve your purpose.
I'd suggest looking at libraries that support mDNS service discovery such as mndsjava
Upvotes: 0