Java Sockets
import java.net.*; import java.io.*; public class LowPortScanner { public static void main(String[] args) { //A hostname String host = "localhost"; if (args.length > 0) { host = args[0]; } for (int port = 1; port < 1024; port++) { try { //Open a socket. Socket s = new Socket(host, port); System.out.println("There is a server on port " + port + " of " + host); } catch (UnknownHostException e) { System.err.println(e); break; } catch (IOException e) { // must not be a server on this port } } // end for } // end main } // end PortScanner
socket.getInetAddress()
setPort()
getLocalPort()
returns the number of the local portgetInputStream()
returns an InputStream you can read()
from.getOutputStream()
close()
, shutdownInput()
, shutdownOutput()
5 of 9