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
{
// Where is Program Public variables to be used
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 settings of each called control are omitted here. Code
*/
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
trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1393213