程序运行界面截图:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;namespace CheckIpPORT
{public partial class FrmMain : Form{public FrmMain(){InitializeComponent();}private string netaddress;private int endAdd;//结束地址public int EndAdd{get { return endAdd; }set { endAdd = value; }}private int startAdd;int port;/// <summary>/// 端口/// </summary>public int Port{get { return port; }set { port = value; }}/// <summary>/// 开始地址/// </summary>public int StartAdd{get { return startAdd; }set { startAdd = value; }}//网段public string Netaddress{get { return netaddress; }set { netaddress = value; }}ErrorProvider err = new ErrorProvider();bool CheckInput(){err.Clear();IPAddress ip;if (!IPAddress.TryParse(textBox_ip.Text, out ip)){err.SetError(textBox_ip, "ip不合法!");return false;}int v = 0;if (!int.TryParse(textBox_start.Text, out v)){err.SetError(textBox_start, "格式有误");return false;}startAdd = v;if (!int.TryParse(textBox_end.Text, out v)){err.SetError(textBox_end, "格式有误");return false;}endAdd = v;if (endAdd < startAdd){err.SetError(textBox_end, "终止ip不能大于起始ip");return false;}if (!int.TryParse(textBox_port.Text, out v)){err.SetError(textBox_port, "端口不合法");return false;}else{if (v <= 0 || v >= 65535){err.SetError(textBox_port, "端口不合法");return false;}}port = v;return true;}Thread th;private void button1_Click(object sender, EventArgs e){if (CheckInput()){string ipCheck = textBox_ip.Text.Substring(0, textBox_ip.Text.LastIndexOf('.'));netaddress = ipCheck;this.button1.Enabled = false;this.button1.Text = "正在检测,请稍后";this.button2.Enabled = true;th = new Thread(new ThreadStart(CheckStatus));th.IsBackground = true;th.Start();//MessageBox.Show(ipCheck);}}void SyncTimeOut(Socket client){int i = 2, k = 0;Thread th2 = new Thread(new ThreadStart(() =>{while (true){Thread.Sleep(1000);k++;if (k >= i){try { client.Close(); }catch { }}}}));th2.IsBackground = true;th2.Start();}void CheckStatus(){bool b = false;string ips = string.Empty;for (int i = startAdd; i <= endAdd; i++){ips = netaddress + "." + i;IPAddress ip = IPAddress.Parse(ips);try{IPEndPoint point = new IPEndPoint(ip, port);Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);SyncTimeOut(sock);sock.Connect(point);b = true;//Console.WriteLine("连接端口{0}成功!", point);}catch (SocketException e){if (e.ErrorCode != 10061){Console.WriteLine(e.Message);}//Console.WriteLine("连接{0}失败", i);b = false;}//listView1.Items.Add(new ListViewItem(new string[] { ips, port.ToString(), b ? "开启" : "未开启" }));listView1.Invoke(new delBandData(bindData), ips, b);}this.button1.Enabled = true;this.button1.Text = "开始检测";}public delegate void delBandData(string ip, bool b);void bindData(string ips, bool b){listView1.Items.Add(new ListViewItem(new string[] { ips, port.ToString(), b ? "开启" : "未开启" }));}private void Form1_Load(object sender, EventArgs e){this.Disposed += Form1_Disposed;}void Form1_Disposed(object sender, EventArgs e){try{th.Abort();}catch (Exception){}}private void button2_Click(object sender, EventArgs e){try{button1.Enabled = true;button1.Text = "开始检测";th.Abort();}catch (Exception ex){}}}class IP_PORT_STATUS{string ip;int port;bool status;public bool Status{get { return status; }set { status = value; }}public int Port{get { return port; }set { port = value; }}public string Ip{get { return ip; }set { ip = value; }}}
}