using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
class begain
{
public static void display()
{
Console.WriteLine("=========================================");
Console.WriteLine("... ... scan ports ... ... ");
Console.WriteLine("=========================================");
}
}
class scanports
{
static int[] ports={21,22,23,25,53,79,80,110,111,135,139,445,1080,1433,1521,3306,3389,4889,5631,8080};
string ip;
public scanports(string host)
{
ip=host;
}
public void Start()
{
ArrayList waitHandles=new ArrayList();
for(int i=0;i<ports.Length;i++)
{
Scaner sc=new Scaner(this,ports[i]);
ScanHandler sh=new ScanHandler(sc.scan);
IAsyncResult iar=sh.BeginInvoke(new AsyncCallback(HandleScanFinish),null);
waitHandles.Add(iar.AsyncWaitHandle);
}
WaitHandle.WaitAll( (WaitHandle[]) waitHandles.ToArray(typeof(WaitHandle)) );
}
private void HandleScanFinish(IAsyncResult iar)
{
System.Runtime.Remoting.Messaging.AsyncResult ar=(System.Runtime.Remoting.Messaging.AsyncResult)iar;
ScanHandler sh=(ScanHandler)ar.AsyncDelegate;
Scaner sc=(Scaner)sh.Target;
bool res=sh.EndInvoke(iar);
if(res)
{
Console.WriteLine("port "+sc.port+" open");
}
}
private delegate bool ScanHandler();
private class Scaner
{
scanports host;
internal int port;
internal Scaner(scanports obj,int p)
{
host=obj;
port=p;
}
public bool scan()
{
try
{
new TcpClient(host.ip,port);
return true;
}
catch
{
return false;
}
}
}
}
class mainclass
{
public static void Main(string[] args)
{
begain.display();
string ip=null;
if(args.Length!=0)
{
ip=args[0];
Console.WriteLine("Scaning,Please wait……");
scanports obj_scanports=new scanports(ip);
obj_scanports.Start();
System.Threading.Thread.Sleep(100);
}
else
{
Console.WriteLine("Usage:scanports1.1 ip");
}
}
}