📄 form1.cs
字号:
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.Sockets;
using System.Net;
using System.Threading;
using System.IO;
namespace 扫描网段计算机端口
{
public partial class Form1 : Form
{
string mask;
IPAddress myIP;
string ip;
//private Thread scanThread;
//decimal startIp;
//decimal endIp;
int i;
int startIp;
int endIp;
int startport;
//int endport;
//private int port;
Thread process;
/*static AutoResetEvent Event1 = new AutoResetEvent(false);
static AutoResetEvent Event2 = new AutoResetEvent(false);
static AutoResetEvent Event3 = new AutoResetEvent(false);*/
delegate void SetTextCallback(string text);
private void SetTxt(string text)
{
if (this.listBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetTxt);
this.Invoke(d, new Object[] { text });
}
else
{
this.listBox1.Items.Add(text); //将text的内容添加到listBox1
}
}
public Form1()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
process = new Thread(new ThreadStart(ipscan));
process.Start();
listBox1.Items.Clear();
listBox1.Items.Add("端口扫描器 啊彬制作");
listBox1.Items.Add(" ");
}
private void ipscan()
{
startIp = (int)num4.Value;
endIp = (int)num5.Value;
mask = num1.Value.ToString() + "." + num2.Value.ToString() + "." + num3.Value.ToString() + ".";
for (i = startIp; i <= endIp; i++)
{
ip = mask + i.ToString();
myIP = IPAddress.Parse(ip);
Thread t = new Thread(new ThreadStart(portscan));
t.Start();
System.Threading.Thread.Sleep(10);
/*if (process.ThreadState == ThreadState.Running)
{
process.Suspend();
}*/
}
//Event1.Set();
}
private void portscan()
{
/*AutoResetEvent[] evs = new AutoResetEvent[1];
evs[0] = Event1;
WaitHandle.WaitAll(evs);*/
startport = Int32.Parse(textBox1.Text);
//endport = Int32.Parse(textBox2.Text);
//if ((startport >= 0 && startport <= 65536) && (endport >= 0 && endport <= 65536) && (startport <= endport))
if (textBox1.Text != "")
{
SetTxt("开始扫描...(可能需要请您等待几分钟)");
/*for (int i = startport; i <= endport; i++)
{
port = i;
//使用该端口的扫描线程
scanThread = new Thread(new ThreadStart(Scan));
scanThread.Start();
//使线程睡眠
System.Threading.Thread.Sleep(100);
}*/
string portIP = ip;
TcpClient objTCP = null;
try
{
objTCP = new TcpClient(portIP, startport);
//SetTxt(portIP + "端口" + startport.ToString() + "开放!");
SetTxt("端口开放!");
}
catch
{
//MessageBox.Show("!!!!!!");
}
}
else
{
//MessageBox.Show("输入错误,端口范围为[0-65536]");
MessageBox.Show("请输入要扫描的端口");
}
/*if (process.ThreadState == ThreadState.Suspended)
{
process.Resume();
}*/
//Event2.Set();
//Event1.Reset();
}
/* private void Scan()
{
int portnow = port;
//IPAddress portIP = myIP;
string portIP = ip;
//done[portnow] = true;
//创建TcpClient 对象,TcpClient 用于为TCP网络服务提供客户端连接
TcpClient objTCP = null;
//扫描端口,成功写入信息
try
{
//用TcpClient对性扫描端口
objTCP = new TcpClient(portIP, portnow);
// lbResult.Items.Add("端口" + portnow.ToString() + "开放!");
SetTxt(ip+"端口" + portnow.ToString() + "开放!");
}
catch
{
}
System.Threading.Thread.Sleep(10);
//Event3.Set();
}*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -