C# TcpClient类实现通信端口的扫描

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;//系统网络编程接口命名空间
using System.Threading;//套接字  IP地:端口

namespace _7003_TcpClient类实现通信端口的扫描
{
    public class PortScan
    {
        //源IP地址和目的IP地址以及源端口号和目的端口号的组合称为套接字。其用于标识客户端请求的服务器和服务。
        public static void Main()
        {
            Console.WriteLine("输入主机名称: ");
            string hostName = Console.ReadLine();//读取输入要扫描的主机/IP地址/或者主机名

            Console.WriteLine("输入开始扫描通信端口: ");
            int startPort = int.Parse(Console.ReadLine());//扫描开始的端口

            Console.WriteLine("输入结束扫描通信端口: ");
            int endPort = int.Parse(Console.ReadLine());//扫描结束的端口

            for (int i = startPort; i <= endPort; i++)//循环连接输入范围内的端口
            {
                System.Net.Sockets.TcpClient myTcpClient = new TcpClient();//实例化一个Tcp客户端
                try
                {
                    ///// 127.0.0.1:
                    //myTcpClient.Connect(hostName, i);//Connect循环扫描当前注解的端口列表(其实就是连接这些端口)
                    ////如果Connect在一定的时间内连接不上对应的(主机IP:端口),就会报错
                    //Console.WriteLine("Port " + i.ToString() + " 目前打开!!");//输出当前打开的端口
                    //myTcpClient.Close();//关闭TCP客户端实例


                    //// 多线程池扫描{ }
                    //TcpConnect端口 myTcpConnect端口 = new TcpConnect端口(hostName, i, myTcpClient);//封装要发送的参数
                    //ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadScan), myTcpConnect端口);
                }
                catch (SocketException e)//如果连接失败
                {
                    Console.Write(e.Message);//e保存了连接失败的信息
                    Console.WriteLine("Port " + i.ToString() + " 关闭!");//输出连接失败的原因
                }
            }
            Console.WriteLine("指定通信端口扫描完成!! ");
            Console.WriteLine("请按[Enter] 离开!! ");
            Console.ReadLine();
        }
        //线程池要委托的方法
        static void ThreadScan(object obj)
        {
            TcpConnect端口 myTcpConnect端口 = (TcpConnect端口)obj;//把发送的结构转换成自己自定义的结构/拆箱
            try
            {
                myTcpConnect端口.myTcpClient.Connect(myTcpConnect端口.hostname, myTcpConnect端口.x);
                Console.WriteLine("Port " + myTcpConnect端口.x.ToString() + " 目前打开!!");//输出当前打开的端口
                myTcpConnect端口.myTcpClient.Close();//关闭TCP客户端实例
            }
            catch (SocketException e)
            {
                Console.Write(e.Message);//e保存了连接失败的信息
                Console.WriteLine("Port " + myTcpConnect端口.x.ToString() + " 关闭!");//输出连接失败的原因
            }

        }
    }
    //由于带有参数只能有一个object ,所以自己新建一个类封装要发送的参数,然后把这个类发送
    //object 的意思大多就是叫你封装啦。
    public class TcpConnect端口
    {
        //封装的参数
        public string hostname;
        public int x;
        public TcpClient myTcpClient;
        public TcpConnect端口(string host, int i, TcpClient tcp)//构造函数
        {
            hostname = host;
            x = i;
            myTcpClient = tcp;
        }
    }
}

 

你可能感兴趣的:(C#,后端)