19 Şubat 2011 Cumartesi

Port Scanner Class

Güzel bir örnek oldu basit ve hızlı..

    public class PortScanner
    {
        public List<int> ActivePorts = new List<int>();
        public List<int> InactiveReports = new List<int>();

        public void Scan(string IP, int startPort, int endPort)
        {
            Socket Sock = null;

            for (int Port = startPort; Port <= endPort; Port++)
            {
                try
                {
                    Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    Sock.Connect(IP, Port);

                    ActivePorts.Add(Port);
                }
                catch (SocketException ex)
                {
                    InactiveReports.Add(Port);
                }
                finally
                {
                    if (Sock!=null)
                    {
                        if (Sock.Connected)
                            Sock.Disconnect(false);
                        Sock.Close();
                    }
                }
            }
        }
    }

Hiç yorum yok:

Yorum Gönder