简体   繁体   中英

Getting connected System in a LAN network-Java

I have a Server-Client program in Java where I need to find the IP's of the client system and assign it to a JLabel . I have a code for finiding out the IP's but it takes much time to find all. Can anyone give me alternative for this code and assigning IP to JLabel so that when I click the JLabel i can communicate with that particular client.

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

    }

This is wrong. Better way is when client connect to server, attach the ip in some holder, and then broadcast it. You do not need to search network for clients, clients must search for your server(by ip and port).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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