2

我有一个使用本地网络上的 web 服务的 Android 应用程序。有一个配置屏幕,用户在其中通知正在运行 Apache Tomcat 的服务器 IP 地址。

我正在寻找一种基于当前连接的 wi-fi 网络自动检测服务器的方法。即:智能手机IP为10.1.1.90,服务器IP为10.1.1.254。

有没有办法做到这一点?我正在考虑使用 ping,但我不知道是否是一个好主意。

4

5 回答 5

3

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

于 2013-02-14T15:22:21.970 回答
2

你想检测“一个 tomcat 服务器”还是“你的 tomcat 服务器”?

我的意思是,你有什么方法来定制你的服务器吗?如果是这种情况,那么您可以在您的服务器上创建一个非常简单的测试页面(比如“Hello”JSP 页面),您的 Android 应用程序可以查找该页面。

如果您的 Android 收到带有 GET 请求的“Hello”结果http://<tomcat_ip>/hello.jsp,那么您可能会假设该 tomcat 在线。

如果您无法添加此测试页面,那么您可以测试服务器应该提供的任何页面。(即使是有时配置不正确的404页面,并显示tomcat版本......)

于 2012-12-31T12:49:32.440 回答
2

Tomcat 响应标头可以包含xpoweredBy字段,该字段将在启用时宣传 Tomcat。然而,出于安全考虑,它通常被禁用,甚至默认禁用。但是,如果您需要准确地自动检测 Tomcat 服务器,您可以重新启用它。另一方面,确实,如果您可以在您的服务器上放置一个网页,您可以简单地放置一个带有商定签名的标记页面。

如果服务器IP未知,我会提出以下方法来检测网络上的服务器:

  • 最直接的方法是进行广播 ping(例如ping -b broadcast_address,可以在此处计算广播地址)。如此配置的所有网络设备都会回复,然后如上所述验证哪一个是服务器。但是,ping 广播地址需要有根电话。另外路由器可能不支持。
  • 您的 DHCP 服务(很可能是您的路由器)通常可以配置为始终为服务器网卡的相同 MAC 地址发出相同的 IP 地址。
  • 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.
于 2012-12-31T12:54:54.620 回答
2

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.

于 2013-02-14T13:42:08.643 回答
0

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;
}
}
于 2013-02-18T06:39:15.787 回答