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!
source
share