[英]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.