new here, I’m working on a program for port scanning, TCP works well, but I don know how to ahieve UDP ports scanning. Say I wanna know whether UDP port XXXX on another host in this LAN is open. will this code do the work? if not, what’s 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 newbie, still learning.
thanks!
UDP is connectionless, so you can’t expect a response packet, necessarily. If the port is closed, you might get an ICMP error message, though there’s no guarantee of this (e.g. a firewall could silently drop the packet).