使用 输入主机IP或域名,输入开始结束端口。 原理 主要使用了ASP.NET 2.0的System.Net.Sockets组件。 StartPort = Convert.ToInt32(numStart.Text); EndPort = Convert.ToInt32(numEnd.Text); ipAdres = txtIP.Text; Thread[] pool = new Thread[(EndPort - StartPort) + 1]; int i = 0; DateTime start = DateTime.Now; // Loop through the ports between start port and end port for (int CurrPort = StartPort; CurrPort <= EndPort; CurrPort++) { Thread th = new Thread(new System.Threading.ParameterizedThreadStart(portAc)); //NOTE: better to leave to system. // th.Priority = ThreadPriority.AboveNormal; th.Start(CurrPort); pool[i] = th; i++; } #region thread pool int k = --i; int retryCount = 0; for (; i >= 0; i--) { if (pool[i].IsAlive) { i = k; retryCount++; continue; } if (retryCount == 1000) { break; } } #endregion #region httpfinger if (http) { // Create a request for the URL. WebRequest request = WebRequest.Create("http://" + txtIP.Text); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; // Get the response. try{ HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string serverType = response.Headers["server"]; if (serverType.Contains("IIS")) { lblServer.Text = "Windows System "; if (serverType.Contains("5.")) { lblServer.Text += "XP/2000"; } if (serverType.Contains("6.")) { lblServer.Text += "2003"; } } if (serverType.ToLower().Contains("apache")) { lblServer.Text += "probably linux"; } lblServer.Text += " " + serverType; } catch(Exception Err){ //sometime which returns 404 and it makes a problem. } } #endregion DateTime end = DateTime.Now; TimeSpan sonuc = end - start; lblzaman.Text = sonuc.TotalSeconds + " total secs";上面是主要的代码,当然我们需要使用线程了,否则,这么多端口的扫描是非常浪费时间的。 public void portAc(object portNoObj) { int portNo = (int)portNoObj; TcpClient TcpScan = new TcpClient(); try { // Try to connect TcpScan.Connect(ipAdres, portNo); if (!TcpScan.Connected) return; // If there's no exception, we can say the port is open log += "Port " + portNo + " open\r\n"; //NOTE: We may include more finger tips to here switch (portNo) { case 80: http = true; break; } try { DataRow dr = dt.NewRow(); dr[0] = "http://www.portsdb.org/bin/portsdb.cgi?portnumber=" + portNo + "&protocol=ANY&String="; dt.Rows.Add(dr); } // Ends Try catch (Exception Err) { throw Err; } } catch { // An exception occured, thus the port is probably closed } } //TcpClient类提供了网络连接、发送和接收的功能,不过这里我们只是使用了它的连接方法。