Java - 在网络中查找服务器 [英] Java - Find Server in network

查看:55
本文介绍了Java - 在网络中查找服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我不知道 ip 时,我想在我的网络中找到我的服务器.这就是我拥有的代码,但是测试所有 IP 确实需要(!)很长时间:

I want to find my Server in my network, when I don't know the ip. So that's the code I have, but it takes really (!) long to test all IPs:

for (int j = 1; j < 255; j++) {
   for (int i = 1; i < 255; i++) {
       String iIPv4 = "192.168." + j + ".";
       try {
          Socket socket = new Socket();
          SocketAddress address = new InetSocketAddress(iIPv4 + i, 2652 );
          socket.connect(address, 5);
          BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          String fromServer;
          while ((fromServer = in.readLine()) != null) {
             if (fromServer.equals("Connected to CC3000")) {
                System.out.println("CC3000 found! : " + iIPv4 + i);
                return iIPv4 + i;
             }
          }
       } catch (UnknownHostException e) {
       } catch (IOException e) {
       }
   }
}

那么,找到服务器的更好方法是什么?

so, whats a better way to find the server?

问候

推荐答案

我认为多线程可以在这里帮助您,因为您不想等待每个连接建立或失败.

您可以尝试一次说出数百个套接字.此外,检查 IP 是否可访问可能会为您节省一些时间.

You can try say hundreds of sockets at once instead. Also checking that IP is reachable might save you some time.

下面的代码真的是一个天真而可怕的例子,因为根本没有线程管理,但你得到了我希望的画面.你应该更快地得到结果.

Code below is really a naive and horrible example since there's no thread management at all, but you get the picture I hope. You should get the result much faster.

public static void findLanSocket(final int port, final int timeout) {
    List<Thread> pool = new ArrayList<>();

    for (int j = 1; j < 255; j++) {
        for (int i = 1; i < 255; i++) {
            final String iIPv4 = "192.168." + j + "." + i;

            Thread thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    System.out.println("TESTING: " + iIPv4);

                    try {
                        // First check if IP is reachable at all.
                        InetAddress ip = InetAddress.getByName(iIPv4);
                        if (!ip.isReachable(timeout)) {
                            return;
                        }

                        // Address is reachable -> try connecting to socket.
                        Socket socket = new Socket();
                        SocketAddress address = new InetSocketAddress(ip, port);
                        socket.connect(address, timeout);

                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String fromServer;
                        while ((fromServer = in.readLine()) != null) {
                            if (fromServer.equals("Connected to CC3000")) {
                                System.out.println("CC3000 found! : " + iIPv4);
                            }
                        }

                    } catch (UnknownHostException e) {
                    } catch (IOException e) {
                    }
                }
            });

            pool.add(thread);
            thread.start();
        }
    }

    // Wait for threads to die.
    for (Thread thread : pool) {
        try {
            if (thread.isAlive()) {
                thread.join();
            }
        } catch (InterruptedException ex) {
        }
    }
}

这篇关于Java - 在网络中查找服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆