繁体   English   中英

在LAN网络中连接系统-Java

[英]Getting connected System in a LAN network-Java

我在Java中有一个Server-Client程序,我需要在其中找到客户端系统的IP's并将其分配给JLabel 我有一个用于确定IP's代码,但是要花很多时间才能找到所有IP's 任何人都可以给我替代此代码并将IP分配给JLabel的方法,以便在单击JLabel时可以与该特定客户端进行通信。

 void get_list() throws IOException
    {
        try {
            InetAddress localhost = InetAddress.getLocalHost();
            // this code assumes IPv4 is used
            byte[] ip = localhost.getAddress();

            for (int i = 1; i <= 254; i++)
            {
                ip[3] = (byte)i;
                InetAddress address = InetAddress.getByAddress(ip);
                if (address.isReachable(1000))
                {
                    System.out.println(address + " machine is turned on and can be pinged");

                    //***********here how do i assign all the valid connected ip to diffrent jlabel's and i need a alternative for this code
                }
                else if (!address.getHostAddress().equals(address.getHostName()))
                {
                    System.out.println(address + " machine is known in a DNS lookup");
                }
                else
                {
                    System.out.println(address + " the host address and host name are equal, meaning the host name could not be resolved");
                }
            }
        } catch (UnknownHostException ex) {
            Logger.getLogger(Test_Frame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

错了 更好的方法是,当客户端连接到服务器时,将IP附加到某个固定器中,然后广播它。 您无需在网络中搜索客户端,客户端必须通过IP和端口搜索您的服务器。

暂无
暂无

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

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