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 ();
}