Simple Port Scanner application using Java
By: Syed M Hussain in Java Tutorials on 2007-09-03
In this article I will explain how to develop a simple port scanner which you can use to scan a host for open ports. A port scanner is a software tool used by network administrators to scan a host network for open ports. This allows the administrator to check their network security.
There are in total 65,535 ports but not every port is used. Below is a few known ports:
Port 20: FTP | Data port
Port 21: FTP | Control (Command) port
Port 23: Telnet | Unencrypted text communications
Port 25: SMTP | Used for e-mail routing between mailservers
Port 80: HTTP | HyperText Transfer Protocol
Above is just a few known ports but there are more and by developing a simple console application in Java we can scan the localhost for any open ports. First lets have a look at the package we need to import into our code.
The java.net package provides the Classes needed for implementing network applications. One of the Classes is the Socket Class, which implements client sockets. We will use this Class to connect to the localhost and scan through a range of ports.
Now let's get started. The port scanner that we will develop will take two command line arguments. The first argument is the start port, this is the port we want to start scanning from. The second argument is the stop port. We will use a For loop to loop through the start and stop ports, each time we increment the loop, we will establish a connection with the localhost and use the incremented loop counter as the port number.
Below is the complete code of the port scanner with line numbers. Remove the line numbers and save the code as PortScanner.java
import java.net.*; public class PortScanner { public static void main(String args[]) { int startPortRange = 0; int stopPortRange = 0; startPortRange = Integer.parseInt(args[0]); stopPortRange = Integer.parseInt(args[1]); for (int i = startPortRange; i <= stopPortRange; i++) { try { Socket ServerSok = new Socket("127.0.0.1", i); System.out.println("Port in use: " + i); ServerSok.close(); } catch (Exception e) { } System.out.println("Port not in use: " + i); } } }
Explanation Of The Code
Now I'm going to explain the code. First on line 4 and 5 I have declared a start and stop variable to hold the start and stop port numbers. Line 6 and 7 simply converts a string number into an integer number using the parseInt() method.
On line 8 we have a For loop, this loop uses the startPortRange variable to initialize the For loop. The loop increments until it reaches the stopPortRange. On line 9 we have a Try block, each time the For loop increments the Try block gets invoked.
The Try block creates an instance of the Socket Class. In the constructor of the Socket instance Class, we supply the hostname or IP address and the port number. The following code on line 10 creates an instance of the Socket Class with the localhost IP address. The variable "i" is the current port number.
Socket ServerSok = new Socket("127.0.0.1",i);
If a connection is made on the localhost with the current port number the Try block will print a "Port in use" message. If a connection could not be made on the localhost with the current port number, then "Port not in use" message will be printed to the console. On line 12 we close the Socket connection using the close() method.
We are now at the end of this article. I have very briefly introduced you to the java.net package and the Socket class.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Comments