Android connect to 127.168.1.1, how? - android

I have a server program on my PC which is running and waiting for the Android client.
I set these in my Android code:
mHost = "127.168.1.1"; //or getLocalIpAddress()?
mPort = 5000;
mSocket = new Socket(mHost, mPort);
And have these permissions in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
When I try to connect to my PC, I get this logcat output:
10-08 19:18:40.970: E/ex closeInputOutput(8735): java.lang.NullPointerException
10-08 19:18:40.970: E/doInBackground Exception(8735): java.net.ConnectException: failed to connect to /127.168.1.1 (port 5000): connect failed: ECONNREFUSED (Connection refused)
I have a router, how can I connect to my PC to 127.168.1.1:5000?
edit:
public static final 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();
String ip4 = inetAddress.getHostAddress().toString();
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip4)) {
Log.d(TAG, "getLocalIpAddress(): " + ip4);
return ip4;
}
}
}
}
catch (Exception e) {
Log.e(TAG, "ServerUtils: getLocalIpAddress(): " + e.getMessage());
}
return null;
}
this is my new Code, this return 192.168.1.104, wich is not working. with my ipconfig I get 192.168.1.102 (that is the correct.)
So in the last code what is wrong? How can I get the correct local IP?

127.168.1.1 is by default in the loopback range, so likely the connection attempt is not leaving your Android device. try to put your server on a 192.168.x.x ip
http://www.tcpipguide.com/free/t_IPReservedPrivateandLoopbackAddresses-3.htm

It may be different in your LAN, but usually the address 192.168.1.1 is the router address (not the host address).
Confirm you host address by running the command:
ipconfig
at command prompt. The IPv4 address will be the host address and the Gateway address the router address. Be sure to use the correct address on both PC and device.
Note: You need to post the relevant part of your code, both on PC and device, if you want more precise help.
--EDITED--
IP address is like a home address, each device has his own. So when you run ipconfig on the windows machine you get the windows machine ip address. When you call the getLocalIpAddress() on device you get the device IP address.
If you want to connect from the device to the PC, you need to:
Run on the PC a program that listens to the port 5000. If there is no program listening on this port, any attempt to connect to this port will be refused.
On device you run a program that connects to 192.168.1.102 (pc IP address) at port 5000

Well that is the problem of Firewall.... Go to your router's firewall, or System's Firewall... and add 5000 Port into exceptions list... inbound and outbound too.... And 2nd problem seems with your I.P address... 127 is not a Usable Class... It is Loop back testing (127.0.0.1 to 127.255.255.254 ...) Change your address from 127 to 192.168.0.1 or 1.1 ...

Related

Android - get the ip of my hotspot host

I am trying to connect 2 devices through sockets, so that they can exchange data. They are also connected via wifi hotspot. I am using Services.
Device 1 is the hotspot (where the ServerSocket is implemented), Device 2 is the one who connects to it (where the Socket is implemented).
I did some research and i am able to get the ip of each one of them (but calculated on their own class). But in order for me to create the client socket, i need the IP Address of the host (the phone that is working as a hotspot) in the other class. I can not get it on the server side, because that part of the code wont be executed, since i am using one phone to create the hotspot network and another one to connect to it.
I know that usually the IP Address of a device that is tethering is generally the same, but i can not trust that, because i gotta make sure it works on all phones.
So, how can i get the ip address of the server (hotspot host) in the client (phone connected to that hotspot) service ?
In the client side you can use dhcp.gateway to get the server side(The one who created hotspot) ip address.
private final WifiManager manager;
private final DhcpInfo dhcp;
private InetAddress getServerIP() {
manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);// gateway -
default gateway IP address
InetAddress serverIP = null;
try {
serverIP = InetAddress.getByName(address);
if(mDebug)
Log.i("Server IP ","" + serverIP.toString());
} catch (Exception e) {
if(mDebug)
Log.e("Cannot find server's IP. Error ","" + e.toString());
}
return serverIP ;
}
Determine the ip of the gateway. Programmatically getting the gateway and subnet mask details. Use the WifiManager.getDhcpInfo().gateway
.

Android Server Socket

I am unable to reach the Android Server on the emulator from a program on my desktop, how do I solve it?
Some code (from How to find LAN ip address of android device?):
public static ArrayList<String> getSelfIP(){
try {
ArrayList<String> ipList = new ArrayList<>();
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()) {
ipList.add(inetAddress.getHostAddress().toString());
}
}
}
return ipList;
} catch (SocketException ex) {}
return null;
}
The result is [fe80::5054:ff:fe12:3456%eth0, 10.0.2.15]
What do I have to configure or do to make the emulator reachable by my desktop programs?
I have done the following:
> adb forward tcp:50000 tcp:50000
However, I am unable to access the server through localhost:50000.
Try using the ip address 10.0.2.2.
It is the Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
Take a look at this Android documentation, Section "Using Network Redirection".
Setting up Redirection through the Emulator Console
Each emulator instance provides a control console the you can connect
to, to issue commands that are specific to that instance. You can use
the redir console command to set up redirection as needed for an
emulator instance.
First, determine the console port number for the target emulator
instance. For example, the console port number for the first emulator
instance launched is 5554. Next, connect to the console of the target
emulator instance, specifying its console port number, as follows:
telnet localhost 5554
Once connected, use the redir command to work
with redirection. To add a redirection, use:
add <protocol>:<host-port>:<guest-port>
where <protocol> is either tcp
or udp, and <host-port> and <guest-port> sets the mapping between your
own machine and the emulated system, respectively.
For example, the following command sets up a redirection that handles
all incoming TCP connections to your host (development) machine on
127.0.0.1:5000 and will pass them through to the emulated system's 10.0.2.15:6000:
redir add tcp:5000:6000
In your case the last command would be
redir add tcp:5000:5000
I had this issue once, use 10.0.2.2 IP adress it should solve your problem.

How to connect to AVD

i'm trying to write my own android http server. It's quite OK but i have a problem with my AVD. I don't want to download my app to phone everytime I want to test changes. I would like to connect to my app via AVD.
To get the ip address i'm using this function:
private 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 (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}
on my phone everything works good, but when i run my app on AVD it shows ip: 10.0.2.15
and i'm unable to connect to it.
Is there any way to connect to my app running on AVD ?
If it does matter my app uses port 8080.
Telnet into the device (assuming it is on port 5554):
telnet localhost 5554
At the Android console prompt use a redirect:
redir add tcp:8080:8080
Pointing your browser to 'http://127.0.0.1:8080/' should now send, and receive to the AVD.
Courtesy of:
http://www.rhill.co.uk/?p=35
While I don't know the answer to your problem directly, I do know that when connecting FROM an AVD TO your computer, you have to use 10.0.2.2 because your AVD is essentially behind another "router". It doesn't get a local lan IP from your router. See this question for more information. From this link, he quotes:
Each instance of the emulator runs behind a virtual router/firewall
service that isolates it from your development machine's network
interfaces and settings and from the internet. An emulated device can
not see your development machine or other emulator instances on the
network. Instead, it sees only that it is connected through Ethernet
to a router/firewall.

get Ip address of computers in WIFI network on Android Emulator

I would like to programmatically find the IP addresses of computers which are connected via WiFi to an Android device or emulator. How do I do this?
Can you share the logcat, I suspect there might be some other issue.Try this code (as is) in a sample application to check only if Wi-Fi IP address is working
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = null;
ip = String.format("%d.%d.%d.%d",
(ipAddress & 0xff),
(ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff))
As stated in another topic, the android Emulator works on a virtual private network.
Which means that the emulator is NOT on the same network as your computer, but on a virtual one. No emulator can see other devices, nor other emulators, nor other devices can see the emulators.
Apart from that I have a question:
How can I get the IP address of a hostname using the WifiManager?
For example, my PC is on the same LAN as my android phone (not emulator), and it has a hostname like User-PC. When I try to get the IP with InetAddress.getByName("User-PC"); on a java application, I get the LAN IP like 192.168.1.100, but when I try it on the phone it doesn't work.. Weird thing is I can establish connections if I know the IP, but cant seem to resolve it from the hostname.
Any ideas?
If you want to detect the ip address of the "Emulator" or android device which is connected to any Network then use this code in Your program. it will give you the exact IP Address which the network have assign to your device.
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())
{
String Ip= inetAddress.getHostAddress().toString();
//Now use this Ip Address...
}
}
}
}
catch (SocketException obj)
{
Log.e("Error occurred during IP fetching: ", obj.toString());
}

How to find the IP Address of HTC Incredible on a CDMA Network

How can I find the IP address of the an HTC incredible connected to Verizon's network?
My other question - do I have to make any changes to my App to run it on a cellular network? Till now the App was running on a Nexus One connected to a local Wi-Fi network.
Thanks.
Through mobile network, to check your IP run the command below:
ifconfig rmnet0
or application like 'Android Status' can be handy. Get it from the market.
To get IP address info look at this example. Might need a tweak for your CDMA.
You need not modify anything to get it working on Mobile n/w
You can use NetworkInterface.getNetworkInterfaces();, then addresses = intf.getInetAddresses(); to enumerate all the IP addresses on all the interfaces. You're looking for an address associated with a PDP.
That should work. I'm currently looking for a neater way.
To get the internal IP Address you can use the following snippet of code:
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
byte[] address = inetAddress.getAddress();
}
}
Note: This will return the private network IP Address.
If you want the public IP Address you will need to use STUN (try jSTUN).
Hope that helps!

Categories

Resources