duckLuck
Posts: 2
Joined: Mon Jul 08, 2013 9:36 am

java getting real ip addres

Mon Jul 08, 2013 9:53 am

Hi all,

in my code i?m using the following to get the ip address of the pi:
InetAddress.getLocalHost().getAddress().toString()

this is only returning 127.0.1.1 and not the real ip adress which I need to ping the pi from another computer.
So how do i get the real ip adress from the raspberry with java?

Thx a lot.

User avatar
malakai
Posts: 1382
Joined: Sat Sep 15, 2012 10:35 am
Contact: Website

Re: java getting real ip addres

Mon Jul 08, 2013 11:27 am

Just a guess but I think it is just reading the info from the host file something like

Code: Select all

InetAddress.getLocalHost().getHostAddress().toString()
or

Code: Select all

InetAddress.getHostAddress().toString()


This was helpful to me

Code: Select all

 import java.net.UnknownHostException;

/**
 * Simple Java program to find IP Address of localhost. This program uses
 * InetAddress from java.net package to find IP address.
 *
 * @author Javin Paul
 */
public class IPTest {
 
 
    public static void main(String args[]) throws UnknownHostException {
   
        InetAddress addr = InetAddress.getLocalHost();
     
        //Getting IPAddress of localhost - getHostAddress return IP Address
        // in textual format
        String ipAddress = addr.getHostAddress();
     
        System.out.println("IP address of localhost from Java Program: " + ipAddress);
     
        //Hostname
        String hostname = addr.getHostName();
        System.out.println("Name of hostname : " + hostname);
     
    }
 
}

Output:
IP address of localhost from Java Program: 190.12.209.123
Name of hostname : PCLOND3433

Read more: http://javarevisited.blogspot.com/2012/09/java-program-to-find-ip-address-of.html#ixzz2YS742VHG
http://javarevisited.blogspot.com/2012/ ... ss-of.html
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)

JonathhanRight
Posts: 1
Joined: Mon Jan 01, 2018 1:53 pm

Re: java getting real ip addres

Mon Jan 01, 2018 1:57 pm

Well, for the question you asked here's the code,

Code: Select all

import java.net.InetAddress;
import java.net.UnknownHostException;

public class DisplayHostName
{
   public static void main(String[] args)
   {
      try
      { 
         InetAddress inet = InetAddress.getLocalHost();
         // getHostAddress() - this method returns the IP address
         System.out.println("Machine IP address : " + inet.getHostAddress());
         // getHostName() - this method returns the host name
         System.out.println("Host name : " + inet.getHostName()); 
      } 
      catch(UnknownHostException uhe)
      { 
         System.out.println("Sorry!! Could not find local address and hostname!!"); 
      } 
   }
}
For more on getting IP address in java refer this resource -
http://www.flowerbrackets.com/getting-i ... a-program/

User avatar
DougieLawson
Posts: 33053
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: java getting real ip addres

Mon Jan 01, 2018 3:35 pm

That code doesn't work. It returns 127.0.1.1 (which is defined in /etc/hosts).


I found this example

Code: Select all

import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

public class ListNets {

    public static void main(String args[]) throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))
            displayInterfaceInformation(netint);
    }

    static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
        out.printf("Display name: %s\n", netint.getDisplayName());
        out.printf("Name: %s\n", netint.getName());
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            out.printf("InetAddress: %s\n", inetAddress);
        }
        out.printf("\n");
     }
}  
which works better and gives both the IPv4 and IPv6 addresses.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

clicky
Posts: 222
Joined: Thu Oct 25, 2012 7:34 am

Re: java getting real ip addres

Mon Jan 08, 2018 3:53 pm

Example given by @DougieLawson is the right way of fetching (all) IP addresses of the RPi.
It can be improved a bit by filtering out loopbak and link local addresses:

Code: Select all

if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()) {
...
Whenever you need to 'return' your IP address - consider returning a list of IP addresses (including empty list and list with more than one element) as your RPi can be connected to network using ethernet and wifi at the same time, etc... If you really insist on one and only IP from eth0 interface - then using name 'eth0' when filtering (netint.getName().equals('eth0'))...

Return to “Java”

Who is online

Users browsing this forum: No registered users and 2 guests