如何用c# web编程 探测FTP 端口是否开放

输入主机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类提供了网络连接、发送和接收的功能,不过这里我们只是使用了它的连接方法。

From:http://zhidao.baidu.com/question/53537473.html?si=3

 

 
[收藏] [返回顶部] [打印本页] [关闭窗口]  
 
 
 
 相关主题:  
 
EXCEL文档导入SqlServer 数据库实例
如何用c# web编程 探测FTP 端口是否开放
.net C# csharp 函数重载
 
 网友评论:
正在加载……
 
*评论者:
主页:
标题:
*内容:
【声明】 地球黑匣子网刊载的资讯及其他内容均由网友提供分享 并且纯属作者个人观点,不表示地球黑匣子网同意其说法或描述,仅为提供更多信息,也不构成任何建议。网友转载请注明原作者姓名及出处。如有侵犯到您的版权,请与我们联系,我们会马上进行重新整理!
最新日志列表
人气主题
 
 
 
 
声明:本网站部分内容属网民发布和来自于互联网。对于引用、发布、转载和放置的内容(广告、链接、文字、图像或声音),所产生的所有法律责任,都将由信息归属者或者广告厂商提供者承担,并且由此产生的版权、署名权的异议、纠纷,本网站概不承担任何责任,本站转载素材仅供大家欣赏和分享,切勿做为商业目的使用。
 
Copyright © 地球黑匣子网 2007.06 - 2008   Email:dqhxzcom@163.com
鲁ICP备07501416号   QQ:254212580 网站管理