Getting the IP address of the current machine using Java

后端 未结 17 1777
眼角桃花
眼角桃花 2020-11-22 02:26

I am trying to develop a system where there are different nodes that are run on different system or on different ports on the same system.

Now all the nodes create

17条回答
  •  忘了有多久
    2020-11-22 03:21

    You can use java's InetAddress class for this purpose.

    InetAddress IP=InetAddress.getLocalHost();
    System.out.println("IP of my system is := "+IP.getHostAddress());
    

    Output for my system = IP of my system is := 10.100.98.228

    getHostAddress() returns

    Returns the IP address string in textual presentation.

    OR you can also do

    InetAddress IP=InetAddress.getLocalHost();
    System.out.println(IP.toString());
    

    Output = IP of my system is := RanRag-PC/10.100.98.228

提交回复
热议问题