【问题标题】:How to make sure a system is connected through LAN?如何确保系统通过 LAN 连接?
【发布时间】:2012-04-01 11:02:44
【问题描述】:

在我的 Swing 应用程序(使用 Web-Start)中,我必须手动输入我想要授予应用程序访问权限的机器的 IP 地址,现在在输入 IP 地址时我想检查机器的我输入的 IP 地址仅通过 LAN(通过交换机,而不是路由器)连接到我的机器(充当本地服务器不。因为如果机器不在局域网中,它不应该被授予访问应用程序的权限。

我怎样才能做到这一点?

【问题讨论】:

标签: java swing networking


【解决方案1】:

据我了解您的问题,您需要检查在您的应用中输入的特定 IP 地址是否在客户端主机的直接连接网络上。

如果是这种情况,那么使用普通 ping 将不适合您,因为 ping 将涉及数据包路由。所以即使是路由器后面的主机也会回复。

作为一种解决方法,您可以在 ping 中添加“-t 1”参数,为 ICMP 数据包指定 TTL,这样它们就无法通过路由器。

如果您想在 java 中实现类似的功能,请查看以下示例(您应该根据需要采用它):

public class IsAddressDirectlyConnected {

    private static class Network {
        int network;
        int mask;

        Network(int n, int m) {
            network = n;
            mask = m;
        }
    };

    // list of networks on interfaces of machine this code is being run on
    List<Network> mDirectlyAttachedNetworks = new ArrayList<Network>();

    private int addrBytesToInt(byte[] addr) {
        int addri = 0;
        for (int i = 0; i < addr.length; ++i)
            addri = (addri << 8) | ((int)addr[i] & 0xFF);        
        return addri;
    }

    private void collectLocalAddresses() {
        try {
            Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();

            while (nifs.hasMoreElements()) {
                NetworkInterface nif = nifs.nextElement();
                if (nif.isUp()) {
                    List<InterfaceAddress> ias = nif.getInterfaceAddresses();
                    for (InterfaceAddress ia : ias) {
                        InetAddress ina = ia.getAddress();
                        if (ina instanceof Inet4Address) {
                            int addri = addrBytesToInt(ina.getAddress());
                            int mask = -1 << (32 - ia.getNetworkPrefixLength());
                            addri &= mask;
                            mDirectlyAttachedNetworks.add(new Network(addri, mask));
                        }
                    }
                }
            }
        } catch (SocketException ex) {
            System.err.println("Socket i/o error: " + ex.getLocalizedMessage());
        }
    }

    public boolean isDirectlyAttachedAndReachable(InetAddress address) {
        int checkedAddr = addrBytesToInt(address.getAddress());
        try {
            if (!address.isReachable(1000))
                return false;
        } catch (IOException ex) {
            System.err.println("Failed to check reachability: " + ex.getLocalizedMessage());
            return false;
        }

        for (Network n : mDirectlyAttachedNetworks) {
            if ((checkedAddr & n.mask) == n.network)
                return true;
        }
        return false;
    }

    public IsAddressDirectlyConnected() {
        collectLocalAddresses();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        IsAddressDirectlyConnected iadc = new IsAddressDirectlyConnected();

        if (args.length == 1) {
            try {
                boolean check = iadc.isDirectlyAttachedAndReachable(Inet4Address.getByName(args[0]));
                System.out.println("Given IP is " + (check ? "" : "not ") + "on directly attached network " + (check ? "and " : "or not ")  + "reachable from local host.");
            } catch (UnknownHostException ex) {
                System.err.println("Failed to parse address: " + ex.getLocalizedMessage());
            }
        } else System.out.println("Specify address to check.");
    }
}

【讨论】:

  • -- 说得好@ Vadym,这就是我真正想要的......谢谢:)
【解决方案2】:

只需 Ping 到该地址

Process p = Runtime.getRuntime().exec("ping " + your_ip_address);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

【讨论】:

  • +1 为您的答案,但是,我有一个表格可以在单元格中输入 ip 地址以及主机名、MAC 地址等其他信息以编程方式自动填充其中,现在使用 @ 进行检查987654322@ 和 BufferedReader 对我来说效率不高。 . 有没有更快的解决方案?能快速给我truefalse的答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 2021-05-05
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多