Port Scanner Java implementation

Source: Internet
Author: User
Tags socket window
Program | port scan how to scan the local and remote port of computer system, monitor whether it is turned on or off in many applications, the following is a simple port scanner implemented in Java.

Source Code:



--------------------------------------------------------------------------------


/*
* Created on 2005-3-22
*
* TODO to change the template of this generated file go
* Window-preferences-java-code Style-code Templates
*/

/**
* @author Whandey Connect to Me:whandey@163.com
*
* TODO to change the template of this generated type comment go
* Window-preferences-java-code Style-code Templates
*/
Import java.io.*;
Import java.net.*;
Import java.util.*;


public class Socketport {

public static void Main (string[] args) {

String IP = "159.162.39.27";
string hostname = new string ();

try{//get the target IP address and hostname
InetAddress address = inetaddress.getbyname (IP);
SYSTEM.OUT.PRINTLN (address);
hostname = Address.gethostname ();
SYSTEM.OUT.PRINTLN (hostname);
}
catch (Unknownhostexception e)
{
System.out.println ("Could not find" + IP);

}


Try
{//creat the output file
PrintWriter fout = new PrintWriter (New FileWriter ("PortInf.txt"));
FOUT.PRINTLN ("Information of the Port on" + hostname + "computer");
Fout.println ();

Do ports scan
for (int nport = 25;nport < 30;++nport)
{
Try
{

Socket s = new socket (hostname,nport);
FOUT.PRINTLN ("The port" + Nport + "is open!");

Fout.println ("Connected to" + s.getinetaddress () + ' on port ' + s.getport () + "from port" + s.getlocalport () + "of" + S.getlocaladdress ());
Print the connected socket information
}

catch (IOException E)
{
FOUT.PRINTLN ("The port" + Nport + "is closed!");
}

}
Fout.close ();

}
catch (IOException e) {}

}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.