In Java, how to achieve UDP port scan?

new here, I am working on a program for scanning ports, TCP works well, but I don’t know how to check UDP ports. Say I want to know if the UDP port XXXX is open on another host on this LAN. will this code work? if not, then what is the problem?

protected String scanUDP(InetAddress IP, int port)
{
    try{
        byte [] bytes = new byte[128];
        DatagramSocket ds = new DatagramSocket();
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length, IP, port);
        ds.setSoTimeout(1000);
        ds.send(dp);
        dp = new DatagramPacket(bytes, bytes.length);
        ds.receive(dp);
        ds.close();
    }
    catch(InterruptedIOException e){
        return "CLOSED";
    }
    catch(IOException e){
        return "CLOSED";
    }
    return "OPEN";
}

just a beginner, still learning. thank!

+5
source share
2 answers

UDP is connectionless, so you cannot expect a response packet, of course. If the port is closed, you may receive an ICMP error message, although there is no guarantee on this (for example, the firewall may cancel the packet).

+8
source

UDP- , , TCP.

, python, - UDP- ​​ ICMP Port Unreachable . , , ICMP- , , . , , , . , (, ), ICMP .

+3

All Articles