【发布时间】:2012-11-25 14:00:54
【问题描述】:
我正在尝试在 C# 中创建端口扫描器,到目前为止我有这个:
private void ip()
{
int start = 70;
int end = 80;
string ipString;
if (this.comboBox1.Text == "Domain") {
ipString= Dns.GetHostEntry(txtHost.Text).AddressList[0].ToString();
}
else {
ipString = txtHost.Text;
}
TcpClient asd = new TcpClient();
// MessageBox.Show(ipString);
IPAddress address = IPAddress.Parse(ipString);
for (int i = start; i <= end; i++)
{
try
{
asd.SendTimeout = 3000;
asd.ReceiveTimeout = 3000;
asd.Connect(address, i);
if (asd.Connected)
{
MessageBox.Show("Port " + i + " is open");
}
}
catch
{
MessageBox.Show("Port " + i + " is closed");
}
}
}
但是对于封闭的端口,它有点慢,大约 20 秒,我应该怎么做才能使这个过程更快? 非常感谢。
【问题讨论】:
-
“有点慢”。您应该测量哪个位较慢,这样我们就不必猜测了。
-
您应该考虑异步网络 API。有很多可供选择,但如果您重视自己的理智,请考虑 .net4.5 和 async/await api。同样值得考虑的是,没有一个 .net DNS api 有任何好处。也许可以考虑使用第 3 方选项。见:stackoverflow.com/questions/11480742/…
-
慢,大约 20 秒。
标签: c# sockets web port-scanning