Once again i require some small help on how i can make use of Inet6Address on java.
As i develop a application on getting IPv4 address but now i wanna extend it to IPv6. And it seem that i cant get a IPv6 address and it keep get IPv4 address.
ANd i try to import java.net.Inet6Address;
With the existing code that i have which is show below.
public SocketAddress getInetAddress(){
return channel.getRemote();
}
The channel is using the netty project.
How can i do with Inet6Address?
As i found on this web
http://docs.oracle.com/javase/1.4.2/docs/api/java/net/Inet6Address.html
public InetSocketAddress getInetAddress(){
Inet6Address ipv6 = (Inet6Address)channel.getRemoteAddress.**getAddress();**
return channel.getRemote();
}
Can i also ask if the .getAddress() cant be use, Can anyone help me on this issues?
Like do i need to download a jar file to work on this?
The version of java i am using is java 6.
From Swift
if you are ok - try to use
Java-ipv6
what is the type of your channel? never used 'netty', but in sun's nio world, assuming sa is an instance of SocketAddress, just use sa.getAddress(). if sa is representing a socket on IPv4 address, you will get an instance of Inet4Address, or a Inet6Address when it is a socket on IPv6 address.
add some IPv6 bind code:
InetAddress[] addresses = InetAddress.getAllByName("localhost");
Inet6Address add6 = null;
for(InetAddress add : addresses) {
if (add instanceof Inet6Address) {
add6 = add;
break;
}
}
if (add6==null)
throw new RuntimeException("no IPv6 local address found!");
InetSocketAddress sa=new InetSocketAddress(add6, port);
...
Related
First of all my programming language is Java. I created a simple chat program using sockets. It works pretty good.
I tried it on my computer (localhost) between two terminal instances.
I want to try it out on my laptop, or on another computer. For this I need the client's internal IP address.
How to figure out client's internal IP address using Java?
I specially want to get it out with Java, not using CMD, or something like this. I mean - that's not just a constant string.
In Java, there is a class InetAddress that represents IP Adress (and its corresponding host name, in some cases).
For example, let's get my IP address and my host name:
import java.net.InetAddress;
public class Main {
public static void main(String[] args) {
try {
InetAddress i = InetAddress.getLocalHost();
System.out.println(i); // host name and IP address
System.out.println(i.getHostName()); // name
System.out.println(i.getHostAddress()); // IP address only
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output (in my case):
LLEITE/192.168.1.100
LLEITE
192.168.1.100
Use the below code:
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+ownIP.getHostAddress());`
The following snippet :
<%= InetAddress.getLocalHost() %>
gives out this : Feddy/192.168.42.194
but when I check the website ipchicken , I get this :106.193.214.75
Why the two IP differ ?
106.193.214.75 is a public IP address of your network.
192.168.42.194 is your local IP address - IP of the machine in internal network. Every machine in your network have the same public IP address.
An address 192.168.x.x is for private internal networks only. The fact you can talk to the internet means you also have a public IP address.
Its the job of your router to do network address translation so that your devices on your private network all appear with your public address.
The server is behind NAT which gives it a separate IP address locally compared to the one used on public Internet.
There are several reasons why NAT is used, including security and the limitation of available public IPv4 addresses.
192.168.xx.xx is your local ip on your network. 106.193.xxx is your external IP.
You can get both with the following code:
String hostName = InetAddress.getLocalHost().getHostName();
InetAddress[] addresses = InetAddress.getAllByName(hostName);
for (InetAddress a: addresses) {
System.out.println(a.getHostAddress());
}
The ip 192.168.42.194 is your local ip, it is given to your pc by your router.
The other ip is your WAN ip, it is given by your isp and is the ip address your router gets for connections from the outside world
Because 192.168.42.194 is your private ip, on your private network, and 106.193.214.75 your public one, assigned to your gateway by your ISP.
In JDK 1.6
List<InetAddress> addrs = new ArrayList<InetAddress>();
for(NetworkInterface ni : NetworkInterface.getNetworkInterfaces()) {
if(ni.isUp()) {
for(InetAddress addr : ni.getInetAddresses()) {
addrs.add(addr);
}
}
}
Regards,
One is your local IP address (from your router) and the other one is your IP address over Internet.
192.168 is always from a router
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 set up a static IP and did port forwarding on my notebook, and now I have a static IP address, but it's relatively static, every time I re-start the machine, I get another address, and since I have a "static" IP I can now do Paypal IPN messaging. But how can I get this static IP from my Java program ? One way I can think of is to visit : http://portforward.com/ and on that page it tells me what my external IP is, so I can extract it with Java code, is there any other way that I can do in my Java code to get this info ?
The best solution is probably dynamic DNS. Essentially, you run a program on your computer (or router) that notifies a DNS server when your IP changes. Then, you can just tell PayPal the domain name.
There is a public service you can call with your script to retrieve your external IP address. Bear in mind they have changed the link location once. If you control your own server, you should probably write your own PHP script to simply return the address of the caller to the script.
http://www.whatismyip.com/faq/automation.asp - follow the developers link they provide
import java.net.*;
import java.io.*;
URL myExternalIP = new URL("PUT THE LINK HERE");
BufferedReader in = new BufferedReader(new InputStreamReader(
myExternalIP.openStream()));
String ip = in.readLine(); //you get the IP as a String
System.out.println(ip);
Your own server script is a simple one-liner. Create a file called whatismyip.php and here's the content.
<? echo $_SERVER['REMOTE_ADDR']?>
I see three ways of doing this:
As you discovered, querying an external server what IP you're apparently connecting from. This is simple but you require such a service to be available and, usually, that no transparent proxy messes with your results.
IGD, a sub-protocol of UPnP can give you the result very easily if your port forwarding devices supports it (many do). Google "IGD JAVA" for libraries and samples.
Register for a dynamic DNS service and then lookup your own DNS name.
You can use the NetworkInterface class:
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
while(ifcs.hasMoreElements()){
NetworkInterface ifc = ifcs.nextElement();
System.out.println("Adresses of: "+ifc.getDisplayName());
Enumeration<InetAddress> adresses = ifc.getInetAddresses();
while(adresses.hasMoreElements()){
System.out.println(adresses.nextElement().getHostAddress());
}
}
This snippet will show you all of the interfaces and the IPs bound to them. You will need to look through the list to find the appropriate interface (see also NetworkInterface.getByName(String name)) And then look at the addresses for that interface. Once you have found the appropriate InetAdress you can use that to get the string or byte representation.
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.