Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
局域网Java中的联网系统_Java_Client_Server_Lan - Fatal编程技术网

局域网Java中的联网系统

局域网Java中的联网系统,java,client,server,lan,Java,Client,Server,Lan,我在Java中有一个Server-Client程序,需要找到客户端系统的IP,并将其分配给JLabel。我有一个代码,用于细化IP的,但要找到所有这些代码需要很多时间。是否有人可以为我提供此代码的替代方案,并将IP分配给JLabel,以便当我单击JLabel时,我可以与该特定客户机通信 void get_list() throws IOException { try { InetAddress localhost = InetAddress.ge

我在
Java
中有一个
Server-Client
程序,需要找到客户端系统的
IP
,并将其分配给
JLabel
。我有一个代码,用于细化
IP的
,但要找到所有这些代码需要很多时间。是否有人可以为我提供此代码的替代方案,并将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);
        }

    }
void get_list()引发IOException
{
试一试{
InetAddress localhost=InetAddress.getLocalHost();
//此代码假定使用了IPv4
字节[]ip=localhost.getAddress();

对于(int i=1;i这是错误的。更好的方法是当客户端连接到服务器时,将ip连接到某个支架中,然后广播它。您不需要在网络中搜索客户端,客户端必须搜索您的服务器(通过ip和端口).

我如何知道客户端何时连接到我的服务器?有什么方法知道吗?当我的客户端连接到服务器时,我应该得到通知。我想你误解了我的问题,所有客户端都连接到了服务器。我正在搜索客户端,只是为了使用JButton/JLabel与他们通信。你使用什么服务器?或者你在哪里ve ServerSocket对象正在等待并接受连接?服务器客户端连接的实现是什么?是,我有一个ServerSocket对象正在等待连接。我需要通过单击按钮将客户端系统的详细信息发送到服务器系统。