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());`
Related
I found code to fetch host name from IP address. The code is some what like shown below:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetHostName{
public static void main(String a[]){
try{
InetAddress host = InetAddress.getByName("74.125.68.94");
System.out.println(host.getHostName());
}
catch(UnknownHostException ex){
ex.printStackTrace();
}
}
}
It shows output like:
sc-in-f94.1e100.net
But when i fired this IP address (74.125.68.94) through the browser it opens up Google website.
So my question is how can i fecth URL like http://www.google.com from an IP address rather than displaying sc-in-f94.1e100.net using java?
You can't do that.
Usually, several domains can be reach on same IP. You can just identify the hostname like you already do. This is the name of the machine, equivalent to the IP, which serves the domain.
Moreover, an IP is just an address of a machine which also can serve any domain because it can be a web server or not.
You are actually doing everything right. The thing is that today many host names can live on the same server. This way you can easily convert the host name to the IP address of the server but when you attempt to do the reverse, you only get the name of the server which is in this case sc-in-f94.1e100.net.
Here is an excerpt from hcidata:
In the early years of the Internet, each sub-domain would have a unique IP address so it was common for a host machine to have only one sub domain name. Nowadays, the common practice is to have many sub-domains with the same IP address. It is also common for the domain name to be converted to the IP address of the host machine that runs the www sub domain.
I hope this will clear things up for you.
I am trying to display and store the Public IP address of a system. I used the following code for the same. i.e.,
I have imported following two statements :
import java.net.InetAddress;
import java.net.UnknownHostException;
try {
InetAddress iAddress = InetAddress.getLocalHost();
String currentIp = iAddress.getHostAddress();
System.out.println("Current System's IP address is : " +currentIp);
} catch (UnknownHostException e) {
System.out.println("Catch block executed. So IP address is not displayed");
}
It is displaying the output as :
"Current System's IP address is : 192.168.1.5"
But my system's public IP address (as checked in https://www.whatismyip.com/) is :
115.107.244.81
So how should I fetch and display the public IP address from .JAVA file?
https://www.whatismyip.com -> which will display the public ip address which will be known to the outside world. If you restart your system then you can see different ip address. Eventhough if you are having a static ip address the website won't show your machine ip address.
Java code output shows your system ip address which will be used for inter communication and that is your actual system ip address. you can verify that in shell/command prompt.
if your are using net from local area network then it always start with 192. something ..
Try to connect with other mode of net connection they it will show the public ip address.
Command to get ip address is ipconfig in command prompt in Windows.
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest servletRequest = attributes.getRequest();
ipAddress = String.valueOf(servletRequest.getSession().getAttribute("clientAddress"));
I want to create a super basic Android App that connects to a python server running on my PC but the python server never gets the connection
my java code:
public class WriteToSocket {
Socket sock;
public void Test() {
try {
this.sock = new Socket("PCName", 9871);
} catch (UnknownHostException e) {
System.out.println("Unknown host: PCName");
System.exit(1);
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}
public void Test1(){
try {
this.sock.close();
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}
and
public void onClick(View v) {
WriteToSocket a = new WriteToSocket();
a.Test();
}
and my python server is
import socket
sock = socket.socket()
name = "PCName"
port = 9871
sock.bind((name,port))
sock.listen(1)
s,a = sock.accept()
I expected after the button click for the python server to accept the connection (I also tried changing "PCName" to "127.0.0.1")
I've looked around but nothing helped me so far :S
Bind your server socket to one of the IP addresses of your PC which is accessible from your android, and not to 127.0.0.1. Or alternatively bind it to all available interfaces (0.0.0.0).
Then connect from your android to that IP.
E.g. if your PC has IP address 1.2.3.4 then use this IP in both applications.
Use netstat to see if the port is really open on your PC.
Check to see if your android application has the permission to use the internet (specified in the manifest: "USES_INTERNET" or something like that).
Also your python script discards the connection as soon as it is made.
In python change bind address to 0.0.0.0. It will bind for all IPs attached to your machine. Then in android app change to correct IP of your computer.
IP 127.0.0.1 is a loopback and you can't connect to it from outside of the system.
The android phone doesn't know what PCName is, change "PCName" in the python code back to '127.0.0.1', then in the android project put in the local IP address of the server.
This of course assuming that both the phone and the server are on the same local network.
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);
...
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.