Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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_Sockets_Networking_Network Scan - Fatal编程技术网

Java网络服务扫描器

Java网络服务扫描器,java,sockets,networking,network-scan,Java,Sockets,Networking,Network Scan,我正在尝试编写一个类,该类将扫描本地网络以查找将要运行的服务 问题是,如果地址不是活动的,没有回复,它会挂起它5秒钟以上,这是不好的 我想在几秒钟内完成这个扫描。有人能提供一些建议吗 下面是我的代码部分 int port = 1338; PrintWriter out = null; BufferedReader in = null; for (int i = 1; i < 254; i++){ try {

我正在尝试编写一个类,该类将扫描本地网络以查找将要运行的服务

问题是,如果地址不是活动的,没有回复,它会挂起它5秒钟以上,这是不好的

我想在几秒钟内完成这个扫描。有人能提供一些建议吗

下面是我的代码部分

        int port = 1338;
    PrintWriter out = null;
    BufferedReader in = null;

    for (int i = 1; i < 254; i++){

        try {
            System.out.println(iIPv4+i);
            Socket kkSocket = null;

            kkSocket = new Socket(iIPv4+i, port);

            kkSocket.setKeepAlive(false);
            kkSocket.setSoTimeout(5);
            kkSocket.setTcpNoDelay(false);  

            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
            out.println("Scanning!");
            String fromServer;
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Server here!"))
                    break;
            }

        } catch (UnknownHostException e) {

        } catch (IOException e) {

        }
    }
谢谢你的回答!这是我的代码,供其他任何人查找

        for (int i = 1; i < 254; i++){

        try {
            System.out.println(iIPv4+i);
            Socket mySocket = new Socket();
            SocketAddress address = new InetSocketAddress(iIPv4+i, port);

            mySocket.connect(address, 5);   

            out = new PrintWriter(mySocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
            out.println("Scanning!");
            String fromServer;
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Server here!"))
                    break;
            }

        } catch (UnknownHostException e) {

        } catch (IOException e) {

        }
    }

您可以尝试通过调用显式连接到服务器


您可以尝试通过调用显式连接到服务器


您可以使用noarg构造函数创建一个未连接的套接字,然后使用一个小的超时值进行调用

Socket socket = new Socket();
InetSocketAddress endpoint = new InetSocketAddress("localhost", 80);
int timeout = 1;
socket.connect(endpoint, timeout);

您可以使用noarg构造函数创建一个未连接的套接字,然后使用一个小的超时值进行调用

Socket socket = new Socket();
InetSocketAddress endpoint = new InetSocketAddress("localhost", 80);
int timeout = 1;
socket.connect(endpoint, timeout);

谢谢你的建议,我要试试看!谢谢你的建议,我要试试看!超时以毫秒为单位,因此1秒超时应等于1000。超时以毫秒为单位,因此1秒超时应等于1000。