Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webserver on an android hotspot - what's its IP?

I want to setup an android phone as a wifi-hotspot, then run a webserver on it, and browse it from another phone connected to the hotspot.

My phone isn't rooted, but I have termux, which has many unix utilities. I don't want to install an extra app. (But happy to write one!)

I can view python's simple webserver on http://localhost (on the host), but not on the other phone. I tried using the public-facing IP of the host phone (checking it using whatsmyip-type webpages), but didn't work. Someone said that mobile ISPs prevent this, by mapping different internal and external IPs... but here, it's not going through the ISP, just the hotspot...

I also tried IP addresses from ifconfig and from the wifi controls within android, which does work for netcat - but only the IP of the client phone connected to the hotspot (not the host).

That is, netcat is on the phone connected to the hotspot and listens, then netcat on hotspot phone connects to it. (i.e.role swap: hotspot client is netcat server). Weirdly, the hotspot phone doesn't seem to have an IP (at least, not one I've been able to discover, so far). But it must have one, mustn't it, for netcat to connect...?

Anyway, I want the webserver on the hotspot host, so I need its IP to connect to it... is there a way to get it?

This has been bugging me for ages. Many thanks for any help!

EDIT the answers to this question says it's (almost) always 192.168.43.1. I can't try it right now; will update when I have.

UPDATE
1. 192.168.43.1 works
2. py http.server works fine for regular files (e.g. txt, pdf), but video files seem to require some streaming protocol it lacks. I found lighttpd had this (available within termux using apt install lighttpd). But it needs config (and no eg/default). I used:

$ cat > lighttp.conf
dir-listing.activate = "enable"
server.port = 8000
server.document-root = "MY PATH HERE"
$ lighttpd -D -f lighttpd.conf

The dir listing makes it much easier to use, but obviously no security configured here, so want to be careful what you make available.
3. Android (my 5.1, anyway) needs airplane mode off before you can make it a wifi hotspot - which makes sense for internet access... but here, I want only the client to have access to the host, not have it accessible to the whole internet. So I found you can turn off data-access to prevent that, and the hotspot still works. (There's surely a way to have a hotspot in airplane mode programmatically...)

There you have it! Phone as media server.

like image 705
hyperpallium Avatar asked Dec 01 '17 15:12

hyperpallium


Video Answer


1 Answers

public static String getDeviceIpAddress( ) {
String deviceIpAddress = "###.###.###.###";

try {
    for (Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); enumeration.hasMoreElements();) {
        NetworkInterface networkInterface = enumeration.nextElement();

        for (Enumeration<InetAddress> enumerationIpAddr = networkInterface.getInetAddresses(); enumerationIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumerationIpAddr.nextElement();

           if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4) 
            {
                deviceIpAddress = inetAddress.getHostAddress();

                Log.e(TAG, "deviceIpAddress: " + deviceIpAddress);
            }
        }
    }
} catch (SocketException e) {
    Log.e(TAG, "SocketException:" + e.getMessage());
}

return deviceIpAddress;
}

This will help you i think.

like image 94
greenapps Avatar answered Oct 04 '22 04:10

greenapps