Java Reference
In-Depth Information
privileges (i.e., you are not root on a Unix system; for better or worse, other platforms
allow anyone to connect to low-numbered ports).
TCP ports and UDP ports are not related. Two different programs can use the same
port number if one uses UDP and the other uses TCP. Example 12-4 is a port scanner
that looks for UDP ports in use on the local host. It decides that the port is in use if the
DatagramSocket constructor throws an exception. As written, it looks at ports from
1024 and up to avoid Unix's requirement that it run as root to bind to ports below 1024.
You can easily extend it to check ports below 1024, however, if you have root access or
are running it on Windows.
Example 12-4. Look for local UDP ports
import java.net.* ;
public class UDPPortScanner {
public static void main ( String [] args ) {
for ( int port = 1024 ; port <= 65535 ; port ++) {
try {
// the next line will fail and drop into the catch block if
// there is already a server running on port i
DatagramSocket server = new DatagramSocket ( port );
server . close ();
} catch ( SocketException ex ) {
System . out . println ( "There is a server on port " + port + "." );
}
}
}
}
Here are the results from the Linux workstation on which much of the code in this topic
was written:
% java UDPPortScanner
There is a server on port 2049.
There is a server on port 32768.
There is a server on port 32770.
There is a server on port 32771.
The first port, 2049, is an NFS server. The high-numbered ports in the 30,000 range are
Remote Procedure Call (RPC) services. Along with RPC, common protocols that use
UDP include NFS, TFTP, and FSP.
It's much harder to scan UDP ports on a remote system than to scan for remote TCP
ports. Whereas there's always some indication that a listening port, regardless of appliā€
cation layer protocol, has received your TCP packet, UDP provides no such guarantees.
To determine that a UDP server is listening, you have to send it a packet it will recognize
and respond to.
Search WWH ::




Custom Search