Multithreaded port scanner written by C #

Source: Internet
Author: User
Tags getstream insert connect thread tostring port number visual studio
Program | port Scan | Multithreading Author: She


I was fortunate enough to buy a CD with Visual Studio. NET, and hastily install a set, a very good feeling, learn to write Port scanner one, dedicated to this edition of Friends:
Using System;;
Using System.Drawing;;
using System.Collections;;
Using System.ComponentModel;;
Using System.Windows.Forms;;
Add the following.
using System.Data;;
Using System.Net.Sockets;;
using System.Net;;
Using System.IO;;
using System.Text;;
using System.Threading;;

Namespace Iportscan
{
〈summary〉
Summary description for Form1.
〈/summary〉
public class Form1:System.Windows.Forms.Form
{
This is where the public variables that the program is going to use
public string scanhost = Dns.gethostname ();;//default current native IP
Public Int32 tport = 0;;//Current Connection port number
Public Int32 connstate = 0;;//Scan status
public int portsum = 0;;//Port totals
public bool Endthread = false;;//End State
Public AutoResetEvent Asyncopsaredone = new AutoResetEvent (false);;
Private System.Windows.Forms.Label Label1;;
Private System.Windows.Forms.TextBox txthostname;;
Private System.Windows.Forms.Button CmdExec;;
Private System.Windows.Forms.ListBox loglist;;
public System.Windows.Forms.CheckedListBox portlist;;
Private System.Windows.Forms.Label label2;;
Private System.Windows.Forms.Label label3;;
Private System.Windows.Forms.NumericUpDown snum;;
Private System.Windows.Forms.NumericUpDown eNum;;
Private System.Windows.Forms.CheckBox Showdie;;
Private System.Windows.Forms.Label label4;;
Private System.Windows.Forms.Button button1;;
Private System.Windows.Forms.ToolTip toolTip1;;
Private System.Windows.Forms.StatusBar statusBar1;;
Private System.Windows.Forms.LinkLabel linkLabel1;;
Private System.Windows.Forms.LinkLabel linkLabel2;;
private System.ComponentModel.IContainer components;;
/*
The property settings code for each called control is omitted here
*/
static void Main ()
{
Application.Run (New Form1 ());
}

private void Cmdexec_click (object sender, System.EventArgs e)
{
Int32 Startport = (Int32) snum.value;;
Int32 Endport = (Int32) enum.value;;
if (txthostname.text.length==0)
{
MessageBox.Show ("Please enter the name of a host!") "," System hints ");;
Txthostname.text = Scanhost.tostring ();;
Txthostname.focus ();;
return;;
}
if (Startport〉endport)
{
MessageBox.Show ("error, starting port must be less than end port!", "System Prompt");;
Startport = endPort-1;;
Snum.text = Startport.tostring ();;
Snum.focus ();;
return;;
}

if (cmdexec.text== "&scan")
{
endthread= false;;
cmdexec.text= "&stop";;
}
Else
{
endthread= true;;
cmdexec.text= "&scan";;
}

if (endthread!=true)
{
connstate = 0;;
portsum = 0;;
Scanhost = Txthostname.text;;
Try
{
IPAddress ipaddr = (ipaddress) dns.resolve (scanhost). Addresslist.getvalue (0);;
Txthostname.text = ipaddr. ToString ();;
}
Catch
{
Txthostname.focus ();;
MessageBox.Show ("Please enter the correct host address, this address DNS cannot resolve", "System Prompt");;
return;;
}
LogList.Items.Clear ();;

for (Int32 threadnum = startport;; threadnum〈=endport;; threadnum++)
{
ThreadPool.QueueUserWorkItem (New WaitCallback (Startscan), threadnum);
LOGLIST.ITEMS.ADD ("Scan port:" + threadnum.tostring ());;
}

}
}
public void Startscan (Object state)
{
Int32 port = (Int32) state;;
String tmsg = "";;
String getData = "";;
int lindex = 0;;
int eindex = 0;;
connstate++;//determine Number of threads
if (endthread==true)
{
if (connstate== (Int32) enum.value-(Int32) snum.value))
{
Cmdexec.text = "&scan";;
LOGLIST.ITEMS.ADD ("Scan completed!"); ;
}
Else
{
Cmdexec.text = "&stop";;
LOGLIST.ITEMS.ADD ("Stopping on" +port.) ToString () + "Scan thread for port");;
}
LOGLIST.ITEMS.ADD ("End Thread:" +port.) ToString ());;
Asyncopsaredone.close ();;
}
Else
{
Try
{
TcpClient TCP = new TcpClient ();;
Tcp. Connect (scanhost,port);;
If a connection error is established, the following code will not be executed ...
Portsum + +;;
Lindex = PORTLIST.ITEMS.ADD (port. ToString () + "Port open", false);;
Portlist.selectedindex=lindex;;
Stream sm = tcp. GetStream ();;
Sm. Write (Encoding.Default.GetBytes (Tmsg.tochararray ()), 0,tmsg.length);
StreamReader sr = new StreamReader (TCP. GetStream (), encoding.default);
GetData = Sr. ReadLine ();;
if (lindex!=0&&getdata.length!=0)
{
tmsg = "+-" + port. ToString () + "Port data:" +getdata.tostring ();
Eindex = PortList.Items.Add (tmsg);;//Insert an information record
PortList.Items.Insert (LINDEX+1,TMSG);
PortList.Items.RemoveAt (Eindex);
}
Sr. Close ();;
Sm. Close ();;
Tcp. Close ();;
}
Catch
{
Show necrotic ports
if (Showdie. Checked==true)
{
PORTLIST.ITEMS.ADD (port. ToString () + "Port cannot connect, return data is empty");;
}
}
Finally
{
Thread.Sleep (0);;
LOGLIST.ITEMS.ADD ("End Thread:" +port.) ToString ());;
Asyncopsaredone.close ();;
Statusbar1.text = "Port total:" +portsum.tostring ();;
if (connstate== (Int32) enum.value-(Int32) snum.value))
{
Cmdexec.text = "&scan";;
}
}
}
}

private void Button1_Click (object sender, System.EventArgs e)
{
Application.exit ();;
}


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.