gpt4 book ai didi

java - 如何发现网络上 Tomcat 服务器的 IP 地址?

转载 作者:行者123 更新时间:2023-11-28 21:54:31 25 4
gpt4 key购买 nike

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

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

有办法实现吗?我正在考虑使用 ping,但我不知道这是否是个好主意。

最佳答案

按照我的理解,您需要发现您的 tomcat 服务器的 IP 并使用您的客户端连接它。

我假设,服务器和客户端都在您的控制之下。一种简单的方法是使用 jGroups集群。

  • 你可以让你的 tomcat 被发现
  • 客户端可以使用您提供的集群名称发现它。引用 JChannel Jgroups 使用的 API

我模拟了以下服务器类

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();
}
}

模拟的客户端类

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();
}

客户端将为您提供以下信息

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]

其中 MACHINENAME-47879 是客户端机器和端口 & MACHINENAME-31239 是 tomcat 服务器名称和端口

关于java - 如何发现网络上 Tomcat 服务器的 IP 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14101727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com