TCP port scanning using Java thread Pooling technology

Source: Internet
Author: User
Tags get ip gettext integer thread trim

A simple small program that uses thread pooling technology to implement port scanning (TCP):

The key code is as follows:

Scan this machine
private void Getlocal ()
{
String IP = getip ();
String Portstart = Txportstart1.gettext (). Trim ();
String portend = Txportend1.gettext (). Trim ();
 
if (portstart.length () = 0 | | portend.length () = 0)
Return
 
int s = 0;
int e = 0;
 
try {
s = integer.valueof (Portstart);
E = integer.valueof (portend);
catch (Exception ex) {
Joptionpane.showmessagedialog (NULL, "Port input error");
Return
}
 
Check to see if the port is out of range
if (! (Checkport (s) && Checkport (e)))
{
Joptionpane.showmessagedialog (NULL, "Port should be greater than 0 and less than 65535");
Return
}
 
  
Scann (IP, S, e);
 
Runthread (); Start the thread to monitor whether the scan is complete
}
Private String GetIP ()
{
try {
InetAddress addr = Inetaddress.getlocalhost ();
Return addr.gethostaddress (). toString (); Ip
}
catch (Exception e)
{
Joptionpane.showmessagedialog (NULL, "Get IP Error!");
}
return null;
}
Scan a single IP
private void Scann (String ip, int startport, int endport)
{
Set all buttons to be unavailable
Setbtnedit (FALSE);
Status.settext ("Please wait ...");
String[] Add = {IP, ""};
Table.addrow (add);
exec = Executors.newfixedthreadpool (10);
for (int i = Startport i <= endport; i++)
Exec.execute (New Runsocket (IP, i));
Exec.shutdown ();
  
}

Related Article

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.