+ 6
ip address in localhost networking
i created a local http server in my mobile using an android app(by java networking package).I turned on my mobile hotspot and my server was available in other android devices by searching this url in browser : http://192.168.43.1:6655/ I set up this port(6655) myself. but what's this ip?!! i found it from the internet. If we create a server, how to find either its IP or its hostname? so we can connect to it on other devices via hot-spot/wifi.
44 Respuestas
+ 6
visph Wow... this might be the first time I've been publicly shamed for using `ifconfig` over `ip` on my Android device. 🤣
Indeed... on linux, `ip` is the replacement for `ifconfig`. However, as someone who has worked on Macs, Windows, and linux since the mid '90s, I still swap interchangeably between both commands. I still favor `ifconfig` over `ip` purely based on muscle memory. Perhaps I should run the following on my MacOS to begin to shift my muscle memory:
`brew install iproute2mac`
In the meantime, the following command can be used on Android:
`ip a | grep "inet "`
This has been updated accordingly in my previous post. 😉👌
+ 6
visph It's definitely making me appreciate the strictness of StackOverflow. But I digress and will remove my unrelated comments soon.
+ 5
Mehran Also... the first 2 octets of your IP address is 192.168.0.0 are reserved for private LAN IPs managed by your local network. The following are the reserved ranges for private IPs:
192.168.0.0 to 192.168.255.255
10.0.0.0 to 10.255.255.255
172.16.0.0 to 172.31.255.255
The 3rd octet is often 1 or 43 depending on your router brand. But, they can be any number ranging from 0 through 255.
The 4th octet is unique to your device on the given subnet of your network.
However, the value 1 is often used by the router which is often your default gateway. It's odd that this is the LAN IP address of your device.
+ 5
Mehran Getting the IP address of your Android device isn't difficult at all.
You can get IP addresses programmatically via NodeJS using:
https://code.sololearn.com/ca5A123a1A8A
(IP of remote container used on SoloLearn)
You can run those commands I shared earlier using Termux on your Android device:
https://play.google.com/store/apps/details?id=com.termux
Even simpler than both options, the LAN IP is easily available in your Android UI by going to:
Settings > About phone > Status > IP address
You can also navigate to:
Settings > Connections > Wi-Fi > [Current Connection] > IP address
_
+ 5
Why has this thread become a magnet for gibberish spam? 🤷♂️🤦♂️
+ 4
Mehran To get your WAN IP, you can check:
https://whatismyipaddress.com/
Or an API endpoint like:
https://checkip.amazonaws.com/
----
NOTE: If your device is connected to WiFi, the WAN IP will be shared by all devices on the same router.
Accessing your device via WAN IP from behind a router will require port forwarding rules or some sort of reverse proxy setup on your network.
For LAN IP, on Android, you can go to your WiFi settings, click on the WiFi connection, then view the IP.
You can also see the IP from your router admin if you know your way around that interface.
You also have the loopback IP, which is active even when disconnected from any network. That IP is 127.0.0.1 and is almost always mapped to localhost or whatever has been set in the hosts file of your device.
Using a terminal emulator like Termux, you can check this using:
cat /system/etc/hosts
You can get bound IP4 addresses using:
ip a | grep "inet "
ip route get 1.1
or
hostname
hostname -i
or (legacy)
ifconfig | grep "inet "
+ 4
David Carroll, Martin Taylor
'ifconfig' is the old way command...
the modern way command is 'ip' ^^
https://p5r.uk/blog/2010/ifconfig-ip-comparison.html
+ 3
a device has usually one or more physical net interface (card)...
private ip (for each interface on local network) can be set automatically (dhcp) or manually...
public ip (one for any interface connected to internet) is automatically assigned by your internet provider (some receive fixed ip, other receive ip wich change regularly)...
public ip can be retrieved as said by CarrieForle and access must usually be explicitly authorized for the port used (security)
you could check private ip(s) of your device by using 'ip addr' command in a terminal on Linux / OSX (similar command must exist on Windows) or by searching inside network configuration at GUI...
on android, 'ip addr' should also work if you have a terminal emulator, else you must search inside device configuration (location may depend on android version)...
hostname can be used in place of ip address, but usually require to have a name server installed ^^
+ 3
Mehran and another people which gave my answer - points. What for problem do you see in my answer?
You are writing complete nonsense here. His external IP address is assigned by his provider.
+ 3
visph David Carroll Martin Taylor JaScript
and probably others in future.
thanks Your answers were really helpful.
+ 3
Mehran To clarify... your Android device is:
- the WiFi router
- and the internet gateway
- and the host for your Java service
I'm assuming your hotspot connection is through a mobile network.
Also...
1. Since your Android device is also your router, they share the same private and public IP. This explains why you are able to access your phone via 192.168.43.1 - which typically is used by the router.
2. Devices connected to your hotspot can also access that private IP as they are all on the same network. So far... nothing new here.
You might be able to access that port via your public IP if you allow inbound traffic via your device's firewall.
However, your mobile provider might be blocking all inbound ports from an external firewall, which will make it impossible to override from your phone.
So... this could be tough to resolve using a hotspot.
+ 2
on some platform (ie: windows), localhost can be resolved to private ip... but in most of cases, localhost is mapped to127.0.0.1 ip, and you cannot access the private ip through it, unless your system also map it to ^^
anyway, resolving hostname (localhost) is quite tricky ;P
+ 2
private ip can be seen by running 'ip addr' on linux/osx/android in a terminal... I don't know the command on windows (search on internet)
alternatively, you can see your private ip address by running network configuration tools (GUI), but this highly depends on your os to know the exact steps ^^
private ip are use on a local network... to access from outside (ie: by connecting to internet) you must use public ip and allow access to targeted port ;P
+ 2
Mehran read again my first answer, wich you should have skipped: I tell the same thing more deeply ;)
+ 2
Martin Taylor visph David Carroll
I appreciate your explanation.
And I read them many times.
But there seems to be no simple way to get an IP address.
look to this simple code
//it is local network not global.
//on server side
ss=new ServerSocket(port);
//on client side
s=new Socket(hostname or ip:string, port) ;
note: "localhost" or "127.0.0.1" only works in local machine
As we know on Android can not be used cmd.
So how do I find (programmatically) the IP or hostname and tell the client to connect.
no only android matter for me. also mention other platforms if you know
+ 2
Martin Taylor "quite impossible" is not "completly impossible" (or that's I doesn't enough known english, wich is not my mother tongue ^^)
"through program, finding the private ip is quite tricky" mean programatically, not by using OP eyes by navigating to device configuration ;P
+ 2
most used private ip are of the range 192.168.x.x, some other routers use 10.x.x.x, and very few uses 172.16-31.x.x (for no professional/society local networks, but for home/family local networks)
last numbers are usually attributed from lowest to highest free ones in the range allowed for dhcp on the router (fixed address must not be in same range)... once a device has been attributed an ip, the router memorize it and try to reaffect the same ip at next connection... but sometimes another device has been connected before and have received that ip, so your device may get another one instead of the previously attributed...
that's why service servers works easiest with fixed ip (from wich you are sure to always connect with same private ip, without the risk to have dhcp attributing it to another device)
hostnames are another story: each device has one wich you could decide... that hostname is used to map to ip, but there are many tools to do so... as said by Martin Taylor you can update the /etc/hosts ^^
+ 2
visph thanks for that link. I had no idea that /bin/ip existed! Definitely saves a bit of typing 😊
+ 2
David Carroll probably because it's now 8th in trending threads ^^
+ 2
Mehran
the ip(s) given by your code when run in sololearn should be the private ip of the sololearn server (forgot 127.0.0.1, it's the hostname loopback of any device) ^^
to get private ip of your device on your local network you must run your java code locally (from the device you want to get the ip), and hope that it will return more than only the hostname loopback ip ;P