简体   繁体   中英

How to discover the IP address of a Tomcat server on a network?

I have a Android application which consumes a webservice on a local network. There's a config screen where the user inform the server IP address, which is running Apache Tomcat.

I'm looking for a way to auto-detect the server based on the current connected wi-fi network. ie: The smartphone's IP is 10.1.1.90 and the server IP is 10.1.1.254.

Is there a way to achieve this? I'm thinking on using ping, but I don't know if is a good ideia.

The way I understand it, you need to discover IP of your tomcat server and connect it using your client.

I am assuming , both the server and client is in your control. One simple way can be to use jGroups Cluster.

  • You can make your tomcat discoverable
  • Client can discover it using the name of the cluster you have provided .Refer the JChannel API that Jgroups uses

I simulated it making following server class

public class TomcatServer {

    JChannel channel;

    private void start() throws Exception {
        channel = new JChannel(); // use the default config, udp.xml
        channel.connect("TomcatCluster");
    }

    public static void main(String[] args) throws Exception {
        new TomcatServer().start();
    }
}

The simulated client class

public class MobileApp extends ReceiverAdapter {

    JChannel channel;

    private void start() throws Exception {
       channel = new JChannel(); // use the default config, udp.xml
        channel.setReceiver(this);
        channel.connect("TomcatCluster");
        channel.close();
    }

    public static void main(String args[]) throws Exception {
        new MobileApp().start();
    }

The client will provide you following information

GMS: address=MACHINENAME-47879, cluster=TomcatCluster, physical address=xxxxx:0:xxx:xxxx:xxxx:xxxx:xxx:xxxx:xxxx

** view: [MACHINENAME-31239|1] [MACHINENAME-31239, MACHINENAME-47879]

Where MACHINENAME-47879 is the client machine and port & MACHINENAME-31239 is the tomcat server name and port

Do you want to detect "a tomcat server" or "your tomcat server" ?

I mean, do you have any way to custom your server ? If it's the case, then you could create a very simple test page on your server (say a "Hello" JSP page), which your Android application could look for.

If your Android gets a "Hello" result with a GET request on http://<tomcat_ip>/hello.jsp , then you may assume that the tomcat is online.

If you can't add this test page, then you can test any page which the server is supposed to serve. (even a 404 page which sometimes is not configured well, and shows the tomcat version...)

Tomcat response headers can contain the xpoweredBy field that would advertise Tomcat if enabled. However it is most often disabled due security considerations, and even disabled by default. You however could re-enable it if you need to auto-detect exactly your Tomcat servers. From the other side, indeed, if you can place a web page on your server, you can simply place a marking page with the agreed signature.

If the server IP is unknown, I would propose the following ways to detect the server on the network:

  • The most straightforward way is to do the breadcast ping ( ping -b broadcast_address where breadcast address can be computed here , for instance). All network devices that are configured so would reply, then verify as explained above which one is the server. However pinging broadcast address requires a rooted phone. Also the router may not support.
  • Your DHCP service (most likely your router) can often be configured to issue always the same IP address for the same MAC address of your server network card.
  • If the server is a desktop computer or laptop, it could show its address as QR code on display. It is possible for a smartphone to scan the code from the screen, and this is way easier than to enter IP address through the touchscreen. QR code can also include auto-generated password for extra security.
  • If there is wireless router with the possible login where both server and client are connected, the internal pages of that router often contain the relevant IP addresses. You would need to implement logging into the router and doing some screen scrapping.

I made an Android app which used a local server in the WLAN. I made the terminal (the phone) broadcast it's own IP address, which the server then picked up.

I used MultiCast class on the phone, which added the ip-address of itself to the payload. The server always has a thread in multicast read class that obains the payload of the packet (which is the terminals ip-address). Set the terminal in datagram read state and send the servers ip-address to terminal.

Maybe are better ways, but a great way to get the ip-addresses of unknown terminals in the network.

The way i had resolved this problem is with the use of enumerations.

public 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 (Exception ex) {
    }
    return null;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM