Parsi Coders

نسخه‌ی کامل: سورس پورت اسکنر سی شارپ
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
[C#] Port Scanner
کد:
/* Port Scanner
*  http://ProjectGhostt.com
*  FreckleS
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Port_Scanner
{
    class Program
    {
        static void Main(string[] args)
        {
            // Scan specific port
            int port = 135;
            if (TestPort(port) == true)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Port {0} is OPEN!", port);
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Port {0} is CLOSED!", port);
            }

            // Scan a range of ports
            for (int i = 1; i < 1000; i++) // for (int i = startPort; i < stopPort; increment port)
            {
                if (TestPort(i) == true)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Port {0} is OPEN!", i);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Port {0} is CLOSED!", i);
                }
            }
        }

        static bool TestPort(int port)
        {
            try
            {
                TcpClient tcp = new TcpClient("127.0.0.1", port);
                if (tcp.Connected == true)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
    }
}