inetaddress

Getting My LAN ip address (192.168.xxxx) (IPV4)

橙三吉。 提交于 2019-12-18 12:17:45
问题 In my android device I am trying to find its IP address(IPV4). If I do the following code InetAddress inet = InetAddress.getLocalHost(); System.out.println(inet.getHostAddress()); //giving me 127.0.0.1 The code is giving me 127.0.0.1. I wanted to get the actual IP 198.168.xx.xx. (In My pc the same code giving me the actual IP though.) 回答1: public static String getIpAddress() { try { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf =

Java: convert int to InetAddress

懵懂的女人 提交于 2019-12-17 22:57:22
问题 I have an int which contains an IP address in network byte order, which I would like to convert to an InetAddress object. I see that there is an InetAddress constructor that takes a byte[] , is it necessary to convert the int to a byte[] first, or is there another way? 回答1: This should work: int ipAddress = .... byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray(); InetAddress address = InetAddress.getByAddress(bytes); You might have to swap the order of the byte array, I can't figure

“localhost” vs 127.0.0.1 java

大憨熊 提交于 2019-12-13 19:20:08
问题 Java is giving 127.0.0.1 as IP for InetAddress.getByName("localhost").getHostAddress() But why java not gives "localhost" for InetAddress.getByName("127.0.0.1").getHostName. For later one I get "127.0.0.1" as host name. Please clarify this. 回答1: The javadoc of InetAddress.getByName(String) states The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is

Java: Get my own ip address in my home network

﹥>﹥吖頭↗ 提交于 2019-12-12 02:26:24
问题 I have find two examples on the web to get the ip address the router has given to my pc. Here is the code: import java.net.InetAddress; import java.net.UnknownHostException; public class tryNet { public static void displayStuff(String whichHost, InetAddress inetAddr) { System.out.println("---------------------"); System.out.println("host: " + whichHost); System.out.println("Canonical host name: " + inetAddr.getCanonicalHostName()); System.out.println("Host Name: " + inetAddr.getHostName());

Find IP of program trying to connect to ServerSocket

自古美人都是妖i 提交于 2019-12-10 17:29:44
问题 Although I searched about it I couldn't find an answer. Let's say I have the following Java code: ServerSocket serve = null; try { server = new ServerSocket(5567); } catch (IOException e) { System.err.println("Problem with port 5567"); System.exit(1); } Socket clientSocket = null; try { clientSocket = server.accept(); } catch (IOException e) { System.exit(1); } When server.accept() is being called the program blocks until someone connects to my server. Is there a way, to be able to find the

Java. InetAddress.getLocalHost returns strange IP

百般思念 提交于 2019-12-10 14:29:53
问题 I'm don't understand, why code below prints 0.0.9.229 instead 127.0.0.1. Can anybody tell me, hot to fix that? String ha = InetAddress.getLocalHost().getHostAddress(); System.out.println(ha); UPD: Code running on Ubuntu /etc/hosts 127.0.0.1 localhost 127.0.1.1 2533 回答1: InetAddress.getLocalHost() doesn't do what most people think that it does. It actually returns the hostname of the machine, and the IP address associated with that hostname. This may be the address used to connect to the

isReachable in Java doesn't appear to be working quite the way it's supposed to

a 夏天 提交于 2019-12-09 12:58:01
问题 I'm using Clojure, but I can read Java, so this isn't a Clojure specific question. This doesn't even seem to be working from Java. I'm trying to implement a bit of a 'ping' function using isReachable. The code I'm using is this: (.isReachable (java.net.InetAddress/getByName "www.microsoft.com") 5000) Translated to Java by a good friend of mine: public class NetTest { public static void main (String[] args) throws Exception{ String host = "acidrayne.net"; InetAddress a = InetAddress.getByName

Fully qualified machine name Java with /etc/hosts

大憨熊 提交于 2019-12-07 01:55:34
问题 I am trying get the fully qualified name of my machine (Windows 7 x64) in Java. On my machine, I've updated the c:\Windows\system32\drivers\etc\hosts file such that it has an entry like this: 10.44.2.167 myserver myserver.domain.com All our systems have an entry in the \etc\hosts file (in the above format) which I cannot change. The following code always returns "myserver" and I am never able to get the fully qualified name. InetAddress addr = InetAddress.getLocalHost(); String fqName = addr

Java Getting IPv4 Address

雨燕双飞 提交于 2019-12-06 02:19:36
问题 Regarding this link where using the codes provided to produce the IP addresses. String ip; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr =

Java Getting IPv4 Address

淺唱寂寞╮ 提交于 2019-12-04 07:42:27
Regarding this link where using the codes provided to produce the IP addresses. String ip; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); ip = addr.getHostAddress(); System.out.println(iface.getDisplayName() + " " + ip); }