Home > Net >  About c # to do a full connection port scanner
About c # to do a full connection port scanner

Time:09-22

using System;
using System.Collections.Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. Linq;
Using System.Net;
Using System. The Text;
Using System. The Threading. The Tasks;
using System.Windows.Forms;
Using System.Net;
Using System.Net.Sockets;
Using System. The Threading;
Using System. Security. Cryptography. X509Certificates;
Using System. Data. SqlTypes;
using System.IO;
Using System. Linq. Expressions;

The namespace FullConnetScaner
{
Public partial class Form1: Form
{
Public Int32 connState=0;
Public int portsum=0;
Public string scanHost;
Public string [] portListArray;
Public AutoResetEvent asyncOpsAreDone=new AutoResetEvent (false);
Public _click ()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls=false;
}


Private void buttonStart_Click (object sender, EventArgs e)
{
If (textIP. Text=="")
{
MessageBox. Show (" please enter a domain name or IP address ", "port scanner," MessageBoxButtons. "OK");
return;
}
ScanHost=textIP. Text;
Try
{
IPAddress IPAddress=(IPAddress) Dns. Resolve (scanHost) expressions such as AddressList. GetValue (0);
TextIP. Text=iPAddress. ToString ();
}
Catch
{
TextIP. Focus ();
MessageBox. Show (" please input the correct host address, this address can't parse ", "port scanner," MessageBoxButtons. "OK");
return;
}
ThreadPool. SetMaxThreads (Convert. ToInt32 (numUpDownThread. Value), the Convert. ToInt32 (numUpDownThread. Value));
For (Int32 threadNum=the Convert. ToInt32 (numUpDownPortStart. Value); ThreadNum<=the Convert. ToInt32 (numUpDownPortEnd. Value); ThreadNum++)
{
ThreadPool. QueueUserWorkItem (new WaitCallback (StartScan), threadNum);
}
Void StartScan (object state)
{
Int32 port=(Int32) state;
String MSG="";
String getDatahttps://bbs.csdn.net/topics/="";
ConnState++;
Try
{
TcpClient TCP=new TcpClient ();
TCP. SendTimeout=TCP. ReceiveTimeout=1000;
TCP Connect (scanHost, port);
 
Portsum++;
MSG=port. The ToString () + "port open";
PortListArray] [portsum - 1=MSG;

The Stream Stream=TCP. GetStream ();
Stream. Write (Encoding. The Default. GetBytes (MSG) ToCharArray ()), and 0, MSG, Length);
StreamReader StreamReader=new StreamReader (TCP GetStream (), Encoding. The Default).
Try
{
GetData=(https://bbs.csdn.net/topics/streamReader.ReadLine);
If (getData Length!=0)
{
MSG=port. The ToString () + "port data:" + getData. ToString (); ;
PortListArray] [portsum - 1=MSG;
TextShowInfo. Items. The Add (MSG);
}
}
Catch Exception (err)
{
TextShowInfo. Items. The Add (err. Message);
}
The finally
{
StreamReader. Close ();
Stream. The Close ();
TCP. The Close ();
Thread.sleep (1000);
}
}
Catch Exception (err)
{
TextShowInfo. Items. The Add (err. Message);
}
The finally
{
Thread.sleep (1000);
AsyncOpsAreDone. Close ();
}
}
}


Private void Form1_Load (object sender, EventArgs e)
{
}
}
}
 


Help you a great god, why do I the stream of the return value is NULL, where can I change
  • Related