using System; using System.Collections.Generic; using System.Net.Sockets; using System.Net; namespace PortScanner { public delegate void ScanCompleteHandler(Object sender, string datainfo); public delegate void ScanFailedHandler(Object sender); public class Scanner { public event ScanCompleteHandler ScanCallback; public event ScanFailedHandler FailedCallback; private string ip; private int port; public Scanner(string ip, int port) { this.ip = ip; this.port = port; } public string IP { get { return this.ip; } set { this.ip = value; } } public int Port { get { return this.port; } set { this.port = value; } } // checks the port public void Checks() { try { IPHostEntry hostEntry = Dns.GetHostEntry(this.ip); IPAddress address = hostEntry.AddressList[0]; IPEndPoint ipe = new IPEndPoint(address, this.port); using (Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { // if a connection to port is succesfull then access is open tempSocket.Connect(ipe); if (ScanCallback != null) { string inf = PortsInfo.GetInfo(this.port); ScanCallback(this, inf); } return; }//using } catch (SocketException) { if (FailedCallback != null) FailedCallback(this); return; }//try } } }