简体   繁体   English

如何确保系统通过LAN连接?

[英]How to make sure a system is connected through LAN?

In my Swing application (with Web-Start) I have to manually enter the IP addresses of the machines whom I want to give access to the application, now at the time of enetring the IP addresses I want to check that the machine's IP address I've entered is connected to my machine ( acting as a local server ) via LAN only (through switch, not a case of router) or not. 在我的Swing应用程序(使用Web-Start)中,我必须手动输入我想要授予应用程序访问权限的机器的IP地址,现在在我要检查机器的IP地址的时候输入IP地址输入已经通过LAN连接到我的机器( 充当本地服务器 )(通过交换机,而不是路由器的情况) 不连接。 Because if the machine is not in LAN, it should not given the permission to access the application. 因为如果机器不在LAN中,则不应授予访问应用程序的权限。

How can I achieve this? 我怎样才能做到这一点?

As far as I understand your problem, you need to check whether particular IP-address entered in your app is on directly attached network for client host. 据我了解您的问题,您需要检查您的应用程序中输入的特定IP地址是否位于客户端主机的直接连接网络上。

If this is the case then using plain ping won't work for you as ping will involve packet routing. 如果是这种情况,那么使用普通ping将无法为您工作,因为ping将涉及数据包路由。 So even hosts behind the routers would reply. 所以即使是路由器后面的主机也会回复。

As a workaround, you could possibly add '-t 1' parameter to ping specifying TTL for ICMP packets so that they are not able to pass through router. 作为一种解决方法,您可以在ping中添加'-t 1'参数,为ICMP数据包指定TTL,以便它们无法通过路由器。

Or have a look on following sample if you want something like this implemented in java (you should adopt it for your needs): 或者看看下面的示例,如果你想在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.");
    }
}

Simply Ping to that address 只需Ping到该地址即可

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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