简体   繁体   English

在LAN网络中连接系统-Java

[英]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 . 我在Java中有一个Server-Client程序,我需要在其中找到客户端系统的IP's并将其分配给JLabel I have a code for finiding out the IP's but it takes much time to find all. 我有一个用于确定IP's代码,但是要花很多时间才能找到所有IP's 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. 任何人都可以给我替代此代码并将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);
        }

    }

This is wrong. 错了 Better way is when client connect to server, attach the ip in some holder, and then broadcast it. 更好的方法是,当客户端连接到服务器时,将IP附加到某个固定器中,然后广播它。 You do not need to search network for clients, clients must search for your server(by ip and port). 您无需在网络中搜索客户端,客户端必须通过IP和端口搜索您的服务器。

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

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