Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Mono PortScanner - C# Code Bank


Mono PortScanner
usage: portscan <ipAddress> <minPort> <maxPort> compiler: gmcs
                //program.cs
using System;
using System.Net;
using System.Net.Sockets;

namespace SharpScanner
{
	class Program
	{
		public static void Main(string[] args)
		{
		    if(args[0] == "help")
		    {
		        Console.WriteLine("Usage: portscan <ip address> <min port> <max port>");
		    }
		    else
		    {
			    string ipAddress = args[0];
			    int minPort = Convert.ToInt32(args[1]);
			    int maxPort = Convert.ToInt32(args[2]);
			    PortScanner scan = new PortScanner(ipAddress, minPort, maxPort);
			    scan.run();
			}
		}
	}
}

//PortScanner.cs
using System;
using System.Net;
using System.Net.Sockets;
 
namespace SharpScanner
{
       class PortScanner
       {
           public int maxPort;
           public int minPort;
           public IPAddress ipAddress;
           private TcpClient scan;
           
           public PortScanner(String ip, int min, int max)
           {
               ipAddress = IPAddress.Parse(ip);
               minPort = min;
               maxPort = max;
               scan = new TcpClient();
           }
           
           public void run()
           {
               for(int currentPort = minPort; currentPort < maxPort; currentPort++)
               {
                   scan = new TcpClient();
                   try
                   {
                       scan.Connect(ipAddress, currentPort);
                       
                       Console.WriteLine("Port: {0} is open", currentPort);
                   }
                   catch
                   {
                    continue;
                   }
               }
           }
       }
}
            
Comments
ellipsis's avatar
ellipsis 12 years ago

This is essentially a copy of netfish's Java port scanner.

sky_x's avatar
sky_x 2 years ago

umm not a fan…this code could’ve been written way better and with private Ipadress. why take risks making it public? :/