在LAN网络中获得连接系统 - Java

时间:2015-04-16 06:46:19

标签: java client server lan

我在Server-Client中有Java个程序,我需要找到客户端系统的IP's并将其分配给JLabel。我有一个用于查找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);
        }

    }

1 个答案:

答案 0 :(得分:0)

这是错误的。更好的方法是当客户端连接到服务器,将ip连接到某个持有者,然后广播它。您不需要在网络中搜索客户端,客户端必须搜索您的服务器(通过ip和端口)。