如何扫描端口

 

SpxImage1_thumb%5B1%5D.jpg

 

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: 访问套接字时出错。有关更多信息,请参见备注部分。 

转载于:https://www.cnblogs.com/zbqy/archive/2007/02/19/652472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值