Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Alma Austin   on Oct 15 In Java Category.

  
Question Answered By: Deloris Harris   on Oct 15

Here is a simple port  scanner that scan  all ports:


import java.net.*;
import java.io.*;

public class ScanPort {

public static void main(String args[]) {
for (int portNumber = 0; portNumber < 65536; portNumber++) {
try {
System.out.println("checking for port " + portNumber + " availability");

/*create a ServerSocket */
ServerSocket serverSocket = new ServerSocket(portNumber);

/*Available portNumbers*/
System.out.println("checking for port " + portNumber + " availability, succeeded");

/*if port number is occupied an IOException will be throwen*/
catch (IOException x) {
System.out.println("ERROR MESSAGE: checking for port " + portNumber +
" availability, failed");
}
}
}
}

see also:
http://java.sun.com/j2se/1.5.0/docs/api/java/net/ServerSocket.html

Share: