如何扫描端口
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net.Sockets;按钮事件
private void button1_Click(object sender, System.EventArgs e)
{//扫描端口int iStart=(int)this.numericUpDown1.Value;
int iEnd=(int)this.numericUpDown2.Value;
try { this.progressBar1.Minimum=iStart; this.progressBar1.Maximum=iEnd;this.richTextBox1.Text="程序开始扫描端口:\n";
this.richTextBox1.Update();for(int i=iStart ;i<iEnd;i++)
{ this.progressBar1.Value=i; TcpClient MyTCP=null; try {MyTCP = new TcpClient(this.textBox1.Text,i);
this.richTextBox1.Text+="端口 " + i.ToString() + " 开放!\n";
this.richTextBox1.Update();}
catch {}
}
this.richTextBox1.Text+="扫描端口结束!\n";
}
catch(Exception Err) {MessageBox.Show("扫描端口操作发生错误,错误信息是:"+Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
public class TcpClient : System.Object
System.Net.Sockets 的成员
摘要:
为 TCP 网络服务提供客户端连接。
public TcpClient ( System.String hostname , System.Int32 port )System.Net.Sockets.TcpClient 的成员
摘要:
初始化 System.Net.Sockets.TcpClient 类的新实例并连接到指定主机上的指定端口。
参数:
hostname: 要连接到的远程主机的 DNS 名。
port: 要连接到的远程主机的端口号。
异常:
System.ArgumentNullException: hostname 为 null。 System.ArgumentOutOfRangeException: port 不在 System.Net.IPEndPoint.MinPort 和 System.Net.IPEndPoint.MaxPort 之间。
System.Net.Sockets.SocketException: 访问套接字时出错。有关更多信息,请参见备注部分。