I was trying to make a jFrame which had a button & a text-area/label, the motive being able to retrieve my systems IP Address, the problem is, when I use this code
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
InetAddress ownIP=InetAddress.getLocalHost();
jTextField1.setText(ownIP.getHostAddress());
}
catch (Exception e)
{
jTextField1.setText(e.getMessage());
}
}
But then this gives me back the loop back IP Address, 127.0.0.1 :(
I have static IP configured on my system, but then too that IP does not show up
I use NetBeans IDE 7.0 & Ubuntu 11.04
You can obtain all IP addresses for your system. Use the NetworkInterface.getNetworkInterfaces() method to retrieve all of the network interfaces. For each of the returned interfaces, use the getInetAddresses() method to retrieve all of the associated addresses.
FWIW, InetAddress.getLocalHost().getHostAddress() gives me my real ip address.
FYI, I ran it from a unit test within Eclipse on a macbook.
Related
I've been having an issue with DNS lookup for IPv6 addresses that I can't seem to find good information about.
I'm trying to lookup an IPv6 address using InetAddress.getByName("ipv6.local.com"). It throws an UnknownHostException error.
The weird part is I know the DNS server can be contacted because this works:
InetAddress.getByName("ipv4.local.com")
I also know the IPv6 record is working because I can run:
nslookup ipv6.local.com
and it properly returns 3ffe:b00:0:1:4678:3eff:fe36:16e8.
Likewise, if I run the following in C++, I get a result with the above address as well:
int errorCode = getaddrinfo("ipv6.local.com", "4242", &hints, &res);
I have also tried Inet6Address.getByName(), but this also throws UnknownHostException. So why do getaddrinfo() and nslookup work and not InetAddress.getByName()?
I am attempting the DNS lookup from an Android device (Galaxy Tab S2 8") running Android 6.0.1 on the same network as the DNS server. The DNS server has a record "ipv4.local.com" pointing to 192.168.0.190, and a record "ipv6.local.com" pointing to 3ffe:b00:0:1:4678:3eff:fe36:16e8.
The DNS server is explicitly set in Wi-Fi settings on the Android device, and is running on 192.168.0.182.
Any ideas?
I am not sure why IPv6 resolution is not working for you. Here is the example of Java code(java version "1.8.0_171") I tested:
package com.myjava.ip;
import java.net.InetAddress;
import java.net.UnknownHostException;
class MyIpByHost {
public static void main(String a[]){
try {
InetAddress host = InetAddress.getByName("ipv6.google.com");
System.out.println(host.getHostAddress());
} catch (UnknownHostException ex) {
ex.printStackTrace();
}
}
}
Output: java com/myjava/ip/MyIpByHost => 2404:6800:4003:c02:0:0:0:8a
This code returns IPv6 for endpoint "ipv6.google.com".
Java run C functions in it's backend to get work done. As you already know getaddrinfo() function of C supports IPv6, but gethostbyname() does not.
You may be running outdated version of java with underlying C function
gethostbyname() which does not support IPv6. I
would suggest you to upgrade Java which will use getaddrinfo().
I have a Android application which consumes a webservice on a local network. There's a config screen where the user inform the server IP address, which is running Apache Tomcat.
I'm looking for a way to auto-detect the server based on the current connected wi-fi network.
i.e: The smartphone's IP is 10.1.1.90 and the server IP is 10.1.1.254.
Is there a way to achieve this? I'm thinking on using ping, but I don't know if is a good ideia.
The way I understand it, you need to discover IP of your tomcat server and connect it using your client.
I am assuming , both the server and client is in your control.
One simple way can be to use jGroups Cluster.
You can make your tomcat discoverable
Client can discover it using the name of the cluster you have provided .Refer the JChannel API that Jgroups uses
I simulated it making following server class
public class TomcatServer {
JChannel channel;
private void start() throws Exception {
channel = new JChannel(); // use the default config, udp.xml
channel.connect("TomcatCluster");
}
public static void main(String[] args) throws Exception {
new TomcatServer().start();
}
}
The simulated client class
public class MobileApp extends ReceiverAdapter {
JChannel channel;
private void start() throws Exception {
channel = new JChannel(); // use the default config, udp.xml
channel.setReceiver(this);
channel.connect("TomcatCluster");
channel.close();
}
public static void main(String args[]) throws Exception {
new MobileApp().start();
}
The client will provide you following information
GMS: address=MACHINENAME-47879, cluster=TomcatCluster, physical address=xxxxx:0:xxx:xxxx:xxxx:xxxx:xxx:xxxx:xxxx
** view: [MACHINENAME-31239|1] [MACHINENAME-31239, MACHINENAME-47879]
Where MACHINENAME-47879 is the client machine and port & MACHINENAME-31239 is the tomcat server name and port
Do you want to detect "a tomcat server" or "your tomcat server" ?
I mean, do you have any way to custom your server ? If it's the case, then you could create a very simple test page on your server (say a "Hello" JSP page), which your Android application could look for.
If your Android gets a "Hello" result with a GET request on http://<tomcat_ip>/hello.jsp, then you may assume that the tomcat is online.
If you can't add this test page, then you can test any page which the server is supposed to serve. (even a 404 page which sometimes is not configured well, and shows the tomcat version...)
Tomcat response headers can contain the xpoweredBy field that would advertise Tomcat if enabled. However it is most often disabled due security considerations, and even disabled by default. You however could re-enable it if you need to auto-detect exactly your Tomcat servers. From the other side, indeed, if you can place a web page on your server, you can simply place a marking page with the agreed signature.
If the server IP is unknown, I would propose the following ways to detect the server on the network:
The most straightforward way is to do the breadcast ping (ping -b broadcast_address where breadcast address can be computed here, for instance). All network devices that are configured so would reply, then verify as explained above which one is the server. However pinging broadcast address requires a rooted phone. Also the router may not support.
Your DHCP service (most likely your router) can often be configured to issue always the same IP address for the same MAC address of your server network card.
If the server is a desktop computer or laptop, it could show its address as QR code on display. It is possible for a smartphone to scan the code from the screen, and this is way easier than to enter IP address through the touchscreen. QR code can also include auto-generated password for extra security.
If there is wireless router with the possible login where both server and client are connected, the internal pages of that router often contain the relevant IP addresses. You would need to implement logging into the router and doing some screen scrapping.
I made an Android app which used a local server in the WLAN. I made the terminal (the phone) broadcast it's own IP address, which the server then picked up.
I used MultiCast class on the phone, which added the ip-address of itself to the payload. The server always has a thread in multicast read class that obains the payload of the packet (which is the terminals ip-address). Set the terminal in datagram read state and send the servers ip-address to terminal.
Maybe are better ways, but a great way to get the ip-addresses of unknown terminals in the network.
The way i had resolved this problem is with the use of enumerations.
public String getLocalIpAddress()
{
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception ex) {
}
return null;
}
}
I'm trying to write a simple program using Java that, given an IP in either version 4 or 6 format, will return its FQDN. The following code works fine when given an ipv4 address, but will only return the given address when an ipv6 address is entered.
InetAddress inet;
try { inet = InetAddress.getByName(theIpAddress); }
catch(UnknownHostException e) { System.out.println("Unknown Host"); return; }
System.out.println(inet.getHostAddress(););
System.out.println(inet.getHostName(););
Whenever I enter an ipv6 getHostName() will only return the same ipv6, even when I know that the ipv6 will resolve to a FQDN. Also, if I enter an ipv6 host name, such as ipv6.google.com, in place of theIpAddress, the exception will occur.
I'm new to this stuff so would appreciate any assistance. Thanks.
The problem was actually the version of Java I was running. Updating Java to 1.6.23, from 1.6.21, allowed ipv6s to resolve to their FQDN.
I've done a quick investigation of what's going on with hostname <-> ipv6 resolution in java 8, Windows 7.
Looks like 'default' NameService does not work with ipv6 at all!
But! Java comes with another, JNDI based NameService implementation called 'dns,sun'.
So, if you enable it using either
-Dsun.net.spi.nameservice.provider.1=dns,sun
or
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
you will get bidirectional ip <-> hostname resolution for v4 and v6 addresses like this
InetAddress.getAllByName(...)
address.getHostName()
More info about java ipv6 you can find here http://docs.oracle.com/javase/8/docs/technotes/guides/net/ipv6_guide/
Try inet.getCanonicalHostName(); which "Gets the fully qualified domain name for this IP address."
If you construct the InetAddress using InetAddress.getByName(), getHostName() will return what you constructed it with. getCanonicalHostName() forces a reverse name lookup.
Using java.net.InetAddress it is not possible to have ipv6 and ipv4 name resolution etc. The bunch of static methods like getByName etc delegate the lookup to instance of Inet4(or 6)AddressImpl that does
public native InetAddress[] lookupAllHostAddr(String hostname) throws UnknownHostException;
Now the fun is a) all these are private/package local so there is not way to inject the impl classes to the InetAddress class b) Inet4(or 6)AddressImpl classes are themselves package local . So there is no way to say , do a ipv4 or ipv6 lookup/name resolution on the fly. I do not get the way all extension points were blocked for these classes making them of really very limited use and flexibility.
The real black-magic happens here , where InetAddress class statically initializes the impls, on what does outcome of method isIPv6Supported() dependent ?? My Linux setup supports ipv6 , i can do dns lookups for ipv6 hostnames like ipv6.google.com.
Will appreciate if anybody can point me to the direction of a good net library in java for ipv4/v6 utilities like lookup etc.
class InetAddressImplFactory {
static InetAddressImpl create() {
Object o;
if (isIPv6Supported()) {
o = InetAddress.loadImpl("Inet6AddressImpl");
} else {
o = InetAddress.loadImpl("Inet4AddressImpl");
}
return (InetAddressImpl)o;
}
static native boolean isIPv6Supported();
}
I've been asked to activate a certain piece of code if i was in my college. So I need to find the iP of where i am to match to my colleges iP. Was wonderng how to do this in java? I have already tried a loop back interface.
By using NetworkInterface.getNetworkInterfaces() and calling getInetAddresses() on each interface, you can see all IP addresses assigned to your computer. To check if you have an IP in your university's range, you could do something like this:
boolean onCampusNetwork() {
for(Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
NetworkInterface iface = ifaces.nextElement();
for(Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements;) {
InetAddress address = addresses.nextElement();
// return true if address is in the university's range; something like:
if(address.toString().startsWith("10.0")) {
return true;
}
}
}
// None of the IP addresses were in the university's range.
return false;
}
I haven't run this code, but it should do what you need.
Isn't there supposed to be some kind of protocol for automatic proxy discovery and configuration? Does your college have this already setup? Then it would be better if your code discovered the correct settings and had an option to override the settings.
There are all kinds of websites out there that will give you your public ip (or the public IP of your gateway, which I assume is what you want). You could tell the program to make an HTTP connection to one of those sites and get the page with the info on it. Since these sites have a very predictable format, the result would be very easy to parse with a regex or two. This only works if you have an internet connection though.
Alternatively, you could have the program try to connect to one of your college's intranet servers. If it can make the connection to a site that is not accessible to the outside world, it's on the LAN.
I have created a list of my friends' names. By clicking on their names one frame should get opened for me and a second one on the machine where the friend has the same application running.Just like the chat window in a messenger application. I use the IP address 127.0.0.1 fort this. Will this wor?
this is my list action performed:
private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
ChatFrame frame = new ChatFrame(client);
frame.setVisible(true);
}
OK, let me see if I got this straight: you're creating something akin to an IM, right?
And you're using a connection on a port at 127.0.0.1?
As we all know the 127.0.0.1 is a loopback address, it will only point to one's machine. No matter what machine.
IF you're trying to create an IM, you need a server to handle the messages, OR know the remote address you're trying to connect (and a server there could be made to provide such address, that's how Yahoo Messenger does).