Author: Xiao Hanxue
I had the honor to purchase a CD of Visual studio. net and had a rush to install one. I felt very good at using it. I learned to write a port scan program and dedicate it to my friends in this version :)
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
{
// The public variables used by the program are stored here.
Public string scanHost = Dns. GetHostName (); // default IP address of the current host
Public Int32 tport = 0; // current connection port number
Public Int32 connState = 0; // scan status
Public int portSum = 0; // total ports
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 setting code of 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 ("enter a host name! "," System prompt ");
TxtHostname. Text = scanHost. ToString ();
TxtHostname. Focus ();
Return;
}
If (startPort> endPort)
{
MessageBox. Show ("error, the starting port must be smaller than the ending 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 ("enter the correct host address, which cannot be resolved by DNS", "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 the 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 scanning thread on" + port. ToString () + "port ");
}
LogList. Items. Add ("End thread:" + port. ToString ());
AsyncOpsAreDone. Close ();
}
Else
{
Try
{
TcpClient tcp = new TcpClient ();
Tcp. Connect (scanHost, port );
// If the connection is incorrectly established, the following code will not be executed ..
PortSum ++;
Lindex = portList. Items. Add (port. ToString () + "Open port", 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
{
// Display the necrotic Port
If (showdie. Checked = true)
{
PortList. Items. Add (port. ToString () + "the port cannot be connected, and the returned data is blank ");
}
}
Finally
{
Thread. Sleep (0 );
LogList. Items. Add ("End thread:" + port. ToString ());
AsyncOpsAreDone. Close ();
StatusBar1.Text = "Total ports:" + portSum. ToString ();
If (connState = (Int32) eNum. Value-(Int32) sNum. Value ))
{
CmdExec. Text = "& Scan ";
}
}
}
}
Private void button#click (object sender, System. EventArgs e)
{
Application. Exit ();
}
Summary: development programs based on the. Net Framework are simple, convenient, and powerful.
Download the code and execution file: http: // 202.103.224.htm/icools/bbs/non-cgi/usr/5_5.zip