I'm sending data from android device to localhost database. To do this I need to use my ip address. I can find my ip address by searching 'ipconfig' on the command prompt. I just noticed that my ip address changed slightly even though im using the same wifi connection. The last digit of my ip address changed. This needed a minor change in my code but I was wonder if there was an android function that returned your computers ip address for you so that the code could look like below. Such a function would also help when using other forms of internet connection that would change your ip address.
HttpPost httpPost = new HttpPost("http://" + ipAddressFunction() + "/linker.php");// home wifi
first get your network-interfaces via:
NetworkInterface.getNetworkInterfaces();
then get the IP(s) va:
getInetAddresses()
Here is a function i made to get my Android Device IP.
//Get The Local IP Addresses
private String GetLocalIPAddress(Boolean LoopBack)
{
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.getHostAddress().contains("."))
{
if(inetAddress.isLoopbackAddress() == LoopBack)
{
return inetAddress.getHostAddress();
}
}
}
}
}
catch (Exception e)
{
Log.d(this.getClass().toString(), "Failed Getting Local IP\n\n" + e.getMessage());
}
return null;
}
This function receives a Boolean parameter, if True, it will return the Local Loopback IP (I.E: 127.0.0.1), if False, it will return the normal IP (I.E: 192.168.0.5)
You can try to connect multiple IP, if have the return data is proved to be a computer IP
Related
I have a java spring MVC web application, I am trying to get the IP address of the client machine who are accessing the application.I had tried the code shown below,
InetAddress addr = InetAddress.getLocalHost();
String ipAddr = addr.getHostAddress();
But this is returning me the IP address of localhost.
I have also tried few other suggestions found online like the one below:
String remoteAddr = "";
if (request != null) {
remoteAddr = request.getHeader("X-FORWARDED-FOR");
if (remoteAddr == null || "".equals(remoteAddr)) {
remoteAddr = request.getRemoteAddr();
}
}
This is also not giving the right IP address. I have also tried the following and nothing has worked for me:
https://www.mkyong.com/java/how-to-get-client-ip-address-in-java/
what is the right way to get request's ip
Nothing is giving me the correct IP address. Is there any solution that I could try to get the correct IP address of the machine that is accessing my application.
You have to read the request from where request is coming, Try this
public void readIp(HttpServletRequest request,HttpServletResponse response){
String ip = request.getRemoteAddr();
System.out.println("ip: "+ip);
}
I'm developing one socket app.
Background :
There is two android device.
Both device is on different network (i.e. one is on mobilenetwork and other is on different mobile network or on wifi).
App is working good when on same wifi network.
App is not working on different network.
I've read about port forwarding but this all ended up with router port forwarding, so this has nothing to do with my app, as my app is like to work on different network.
What I've tried:
Use Socket-communication over different networks [ Still unable to find the answer]
I got server ip address using following method and it gives me ip address:
public String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "Server running at : "
+ inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
text.setText(ip);
return ip;
}
I'm using static port 8080 [May be this is a problem,please suggest me a option if so].
I'm using this lines at client side to connect but soket object returns null on different network:
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
So,Please suggest me what should i use for communicating over
different real android device with different network [app runs on same
network perfectly]?
https://en.wikipedia.org/wiki/TCP_hole_punching
This technique can be used if you're attached to P2P connectivity.
Otherwise, you can get an online server that is accessible to both clients and it can be used to forward messages between the two. Amazon Web Services is a good option for this, you can get one year free trial with a T2.mini machine with a public IP.
https://aws.amazon.com/
I wrote the following code to get the IPv4 address of the eth0 interface I am using on a machine. However the code only finds fe80:x:x:x:xxx:xxxx:xxxx:xxxx which is not returned since I am looking for the IPv4 address.
Here is the code.
interfaceName = "eth0";
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName);
Enumeration<InetAddress> inetAddress = networkInterface.getInetAddresses();
InetAddress currentAddress;
currentAddress = inetAddress.nextElement();
while(inetAddress.hasMoreElements())
{
System.out.println(currentAddress);
if(currentAddress instanceof Inet4Address && !currentAddress.isLoopbackAddress())
{
ip = currentAddress.toString();
break;
}
currentAddress = inetAddress.nextElement();
}
It was messing with the logic where it gets the next element. I had the inetAddress next element being gotten before the while compare was ran. Thus making there be no more elements.
The following code has then fixed the logic
interfaceName = "eth0";
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName);
Enumeration<InetAddress> inetAddress = networkInterface.getInetAddresses();
InetAddress currentAddress;
currentAddress = inetAddress.nextElement();
while(inetAddress.hasMoreElements())
{
currentAddress = inetAddress.nextElement();
if(currentAddress instanceof Inet4Address && !currentAddress.isLoopbackAddress())
{
ip = currentAddress.toString();
break;
}
}
I wish I could create a small application that I print screen the IP address to which the socket is connected.
I'm trying this code:
public void onClick(View v) {
Socket s = new Socket();
String host ="10.10.20.xxxx";
try {
s.connect( new InetSocketAddress( host, 6000 ), 1000 );
InetAddress inetAddress = s.getLocalAddress();
String ip = inetAddress.getHostAddress();
//Now, I would like to have printed out the IP-address
Toast.makeText(getBaseContext(), ip , Toast.LENGTH_SHORT).show();
//But nothing happens
} catch (IOException e) {
e.printStackTrace();
}
}
}
But, I have never seen printed the IP address,
If is necessary I can create a TextView and inside-It put the string ip.
Where am I doing wrong? Thanks!
ulyssessPax:
When you connect/accept to/from a device via TCP sockets, you have the following method from that socket:
socket.getRemoteSocketAddress().toString()
it gives you the remote IP address and the port number where it's connected. For example: 192.168.1.30:6000
socket.getLocalSocketAddress()
it gives you the local IP address and the port number where it has established the connection. For example: 10.0.2.15:54471
Hope it's what you're looking for.
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() && !inetAddress.isLinkLocalAddress()) {
System.out.println(inetAddress.getHostAddress());
}
}
}
This returns me all the local IP Addresses. If I am connected to GPRS it returns me another Private Class A IP address and I want to filter this out(as I don't know what this network is or how it works).
I basically need the WiFi/WiFi HotSpot/USB HotSpot/WiFi Direct IP addresses.
Will this GPRS interface be connected to only one device(mine)?