用c# web编程端口是否开放

使用

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

转载于:https://www.cnblogs.com/nibulu/archive/2011/08/04/2127699.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值