簡體   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