从给定的IP地址获取所有打开的端口 [英] get all open ports from given ip address
本文介绍了从给定的IP地址获取所有打开的端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从C#3.0中列出系统的所有开放端口.应该快速运行
我正在尝试通过此代码,但是它非常慢
How to list out all open ports of the system from C#3.0.It should fast
I am trying by this code but it is very slow
for (int CurrPort = 1; CurrPort <= 25; CurrPort++)
{
TcpClient TcpScan = new TcpClient();
try
{
// Try to connect
TcpScan.Connect("192.168.0.1", CurrPort);
// If there's no exception, we can say the port is open
MessageBox.Show("Port " + CurrPort + " open");
}
catch
{
// An exception occured, thus the port is probably closed
MessageBox.Show("Port " + CurrPort + " closed");
}
}
推荐答案
端口扫描程序通常并行扫描,因此您可以执行以下操作(.net 4):
Port scanners usually scan in parallel so you can do the following (.net 4):
List<Task> tasks = new List<Task>();
for(int i =1;i<65535;i++)
{
Task t = Task.Factory.StartNew( () => portscan(i));
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());
这篇关于从给定的IP地址获取所有打开的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文