Feedback

Asynchronous Portscanner

Sprache: C#

Eine einfache und effiziente Methode einen Port zu scannen. Standardmäßig ist das Protokoll auf TCP und der Timeout auf 5000ms gesetzt. Aufruf: [code]//Scan port 22@127.0.0.1 String ipAddress = "127.0.0.1"; int port = 22; IPAddress ip=IPAddress.Parse(ipAddress); IPEndPoint ipEo = new IPEndPoint(ip,port); if (ScanPort(ipEo)) { //Port 22 is open! }[/code]
bool ScanPort(IPEndPoint ipEo, ProtocolType protocol = ProtocolType.Tcp, int timeOut = 5000)
{
	Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, protocol);
	IAsyncResult result = socket.BeginConnect(ipEo, null, null);
	return result.AsyncWaitHandle.WaitOne(timeOut, true);
}
bool ScanPort(IPEndPoint ipEo, ProtocolType protocol = ProtocolType.Tcp, int timeOut = 5000)
{
	Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, protocol);
	IAsyncResult result = socket.BeginConnect(ipEo, null, null);
	return result.AsyncWaitHandle.WaitOne(timeOut, true);
}