欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

C# 单ip端口扫描工具 (多线程)

程序员文章站 2024-01-19 12:16:16
using System; using System.Collections.Generic; using System.ComponentModel; us...
using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  using System.Diagnostics;
 
  namespace 端口扫描
  {
      public partial class Form1 : Form
      {
          ThreadStart threadstart;
          Thread[] thread;
          int port;         //当前正在扫描的端口
          int portmax;      //扫描端口最大值
          
          public Form1()
          {
              InitializeComponent();
          }
 
          public bool IsPortOpen(string ip, int port)       //判断某一计算机端口是否处于打开状态
          {
              try
              {
                  TcpClient client = new TcpClient();       //创建一个TcpClient实例
                  IPAddress address = IPAddress.Parse(ip);  //转化string类型的ip地址到IpAddress
                  client.Connect(address, port);            //连接服务器address的port端口
                  client.Close();                           //连接成功立即断开
                  return true;
              }
 
              catch (Exception e)
              {
                  return false;
              }
          }
 
          delegate void Show_Change(string input);
          public void ShowProc(string input)
          {
              this.lab_show.Text = input;
          }
 
          public void ScanThread()
          {
              while (true)
              {
                  int nowport;
                  lock (this)
                  {
                      if (port > portmax)
                          return;
                      else
                      {
                          //lab_show.Text = port.ToString();
                          this.BeginInvoke(new Show_Change(ShowProc), new object[] { port.ToString() });
                          nowport = port;
                          port++;
                      }
                  }
                  if(IsPortOpen(txt_ip.Text,nowport))
                      listBox_Return.Items.Add(nowport.ToString() + "端口............开放");
              }
          }
 
          private void btn_scan_Click(object sender, EventArgs e)
          {
              listBox_Return.Items.Clear();
              int ThreadCount = (int)numericUpDown_ThreadCount.Value;  //线程数量
              thread = new Thread[ThreadCount];
              threadstart = new ThreadStart(ScanThread);
              for (int i = 0; i < ThreadCount; i++)
              {
                  Thread t = new Thread(threadstart);
                  thread[i] = t;
              }
 
 
              portmax = (int)numericUpDown_Max.Value;
              port = (int)numericUpDown_Min.Value;
              for (int i = 0; i < ThreadCount; i++)
              {
                  thread[i].Start();
              }
          }
 
          private void Form1_Load(object sender, EventArgs e)
          {
              lab_show.Text = "";
          }
 
          private void Form1_FormClosed(object sender, FormClosedEventArgs e)
          {
              Application.Exit();
          }
 
          private void btn_ping_Click(object sender, EventArgs e)             //ping按钮事件
          {
              string command = @"ping "+txt_UrlOrIp.Text;
              txt_ping.Text = RunCmd(command);
          }
 
          
          public string RunCmd(string command)                                 //运行cmd命令,并返回结果字符串
          {
              Process p = new Process();
              p.StartInfo.FileName = "cmd.exe";
              p.StartInfo.Arguments = "/c" + command;
              p.StartInfo.UseShellExecute = false;
              p.StartInfo.RedirectStandardError = true;
              p.StartInfo.RedirectStandardOutput = true;
              p.StartInfo.RedirectStandardInput = true;
              p.StartInfo.CreateNoWindow = true;
              p.Start();
              return p.StandardOutput.ReadToEnd();
          }
 
 
          int[] port_array = { 21, 23, 80, 1433, 3306, 3389 };        //常用端口
          string ip;
          public void Scan()
          {
              for (int i = 0; i < port_array.Length; i++)
              {
                  if (IsPortOpen(ip, port_array[i]))
                  {
                      listBox1.Items.Add(port_array[i].ToString() + "端口.............开放");
                      listBox1.Items.Add("");
                  }
                  else
                  {
 
                      listBox1.Items.Add(port_array[i].ToString() + "端口.............关闭");
                      listBox1.Items.Add("");
                  }
              }
          }
          private void btn_Start_Click(object sender, EventArgs e)    //扫描计算机常用端口
          {
              listBox1.Items.Clear();
              ip = txt_IpAlways.Text;
              ThreadStart ts = new ThreadStart(Scan);
              Thread th = new Thread(ts);
              th.Start();
          }   
 
      }
  }