using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;namespace MyListenForm
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
// private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button ScanButton;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
public Int32 tport = 0; //当前连接端口编号 
public Int32 connState = 0; //扫描状态 
public int portSum = 0 ; //端口总计 
public bool endThread = false; //结束状态 
private Form2 myLogList; public AutoResetEvent asyncOpsAreDone = new AutoResetEvent(false);
private System.Windows.Forms.NumericUpDown StartNum;
private System.Windows.Forms.NumericUpDown EndNum;
private System.Windows.Forms.ListBox portList;
private System.Windows.Forms.CheckBox showdie;
public static System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.CheckBox checkBox1;

//private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.InitButton1();
this.connState=0;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ScanButton = new System.Windows.Forms.Button();
this.StartNum = new System.Windows.Forms.NumericUpDown();
this.EndNum = new System.Windows.Forms.NumericUpDown();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.portList = new System.Windows.Forms.ListBox();
this.showdie = new System.Windows.Forms.CheckBox();
this.myLogList = new MyListenForm.Form2();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
((System.ComponentModel.ISupportInitialize)(this.StartNum)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.EndNum)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
// 
// ScanButton
// 
this.ScanButton.Location = new System.Drawing.Point(320, 64);
this.ScanButton.Name = "ScanButton";
this.ScanButton.Size = new System.Drawing.Size(88, 23);
this.ScanButton.TabIndex = 1;
this.ScanButton.Text = "&Scan";
this.ScanButton.Click += new System.EventHandler(this.ScanButton_Click);
// 
// StartNum
// 
this.StartNum.Location = new System.Drawing.Point(80, 64);
this.StartNum.Maximum = new System.Decimal(new int[] {
 65535,
 0,
 0,
 0});
this.StartNum.Name = "StartNum";
this.StartNum.Size = new System.Drawing.Size(80, 21);
this.StartNum.TabIndex = 2;
// 
// EndNum
// 
this.EndNum.Location = new System.Drawing.Point(216, 64);
this.EndNum.Maximum = new System.Decimal(new int[] {
   65535,
   0,
   0,
   0});
this.EndNum.Name = "EndNum";
this.EndNum.Size = new System.Drawing.Size(80, 21);
this.EndNum.TabIndex = 3;
// 
// label1
// 
this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(16, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 4;
this.label1.Text = "端口号";
// 
// label2
// 
this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(176, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(16, 16);
this.label2.TabIndex = 5;
this.label2.Text = "至";
// 
// portList
// 
this.portList.Dock = System.Windows.Forms.DockStyle.Top;
this.portList.ItemHeight = 12;
this.portList.Location = new System.Drawing.Point(0, 0);
this.portList.Name = "portList";
this.portList.Size = new System.Drawing.Size(432, 148);
this.portList.TabIndex = 6;
// 
// showdie
// 
this.showdie.Location = new System.Drawing.Point(20, 24);
this.showdie.Name = "showdie";
this.showdie.TabIndex = 7;
this.showdie.Text = "showdie";
// 
// myLogList
// 
this.myLogList.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.myLogList.ClientSize = new System.Drawing.Size(432, 280);
this.myLogList.Location = new System.Drawing.Point(22, 22);
this.myLogList.MaximizeBox = false;
this.myLogList.Name = "myLogList";
this.myLogList.Text = "扫描日志";
this.myLogList.Visible = false;
// 
// groupBox1
// 
this.groupBox1.Controls.Add(this.checkBox1);
this.groupBox1.Controls.Add(this.showdie);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.EndNum);
this.groupBox1.Controls.Add(this.StartNum);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.ScanButton);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.groupBox1.Location = new System.Drawing.Point(0, 153);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(432, 100);
this.groupBox1.TabIndex = 10;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "选项";
// 
// checkBox1
// 
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Location = new System.Drawing.Point(136, 24);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 8;
this.checkBox1.Text = "窗体跟随";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 253);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.portList);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "扫描本机端口";
this.Load += new System.EventHandler(this.Form1_Load);
this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged);
((System.ComponentModel.ISupportInitialize)(this.StartNum)).EndInit();
((System.ComponentModel.ISupportInitialize

解决方案 »

  1.   

    )(this.EndNum)).EndInit();
    this.groupBox1.ResumeLayout(false);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Form1 f1=new Form1();
    Form1.button1.Enabled=false;
    //f1.myLogList.Location=f1.Location+(new Size(0,280));
    //MessageBox.Show(f1.myLogList.Location.ToString());
    //MessageBox.Show(f1.Location.ToString());
    //f1.myLogList.StartPosition=System.Windows.Forms.FormStartPosition.Manual;
    f1.myLogList.Show();
    Application.Run(f1);


    }
    private void InitButton1()
    {
    //this.
    button1 = new System.Windows.Forms.Button(); // 
    // button1
    // 
    //this.
    button1.Location = new System.Drawing.Point(320, 32);
    //this.
    button1.Name = "button1";
    //this.
    button1.Size = new System.Drawing.Size(88, 23);
    //this.
    button1.TabIndex = 8;
    //this.
    button1.Text = "显示日志窗口";
    //this.
    button1.Click += new System.EventHandler(this.button1_Click);
    this.groupBox1.Controls.Add(Form1.button1);
    } private void ScanButton_Click(object sender, System.EventArgs e)
    {
    Int32 StartPort=(Int32)this.StartNum.Value;
    Int32 EndPort=(Int32)this.EndNum.Value;
    if(StartPort>EndPort)
    {
    MessageBox.Show("起始端口号大于末端口号","提示");
    return;
    }
    if(this.ScanButton.Text=="&Scan") 

    endThread= false; 
    this.ScanButton.Text= "&Stop"; 

    else 

    endThread= true; 
    this.ScanButton.Text= "&Scan"; 

    if(this.myLogList!=null)
    this.myLogList.listBox1.Items.Clear(); if(endThread!=true) 

    connState = 0; 
    portSum = 0; 
    this.portList.Items.Clear();
    for(Int32 threadNum=(Int32)StartNum.Value;threadNum<=EndPort;threadNum++)
    {
    ThreadPool.QueueUserWorkItem(new WaitCallback(StartScan),threadNum);
    if(this.myLogList!=null)
    this.myLogList.listBox1.Items.Add ("扫描端口:" + threadNum.ToString()); 
    }
    }
    } public void StartScan(object stat)
    {
    Int32 port=(Int32)stat;
    string tMsg = ""; 
    string getData = ""; 
    int lindex = 0; 
    int eindex = 0; 

    this.connState++;
    if(endThread==true) 

    //MessageBox.Show(connState.ToString());
    if(connState==((Int32)StartNum.Value-(Int32)EndNum.Value)) 

    //this.ScanButton.Text = "&Scan"; 
    if(this.myLogList!=null)
    this.myLogList.listBox1.Items.Add ("扫描完毕!"); 

    else 

    this.ScanButton.Text = "&Stop"; 
    if(this.myLogList!=null)
    this.myLogList.listBox1.Items.Add ("正在停止对"+port.ToString()+"端口的扫描线程"); 

    if(myLogList!=null)
    this.myLogList.listBox1.Items.Add("结束线程:"+port.ToString()); 
    asyncOpsAreDone.Close(); 

    else 

    try 

    TcpClient tcp = new TcpClient(); 
    tcp.Connect(IPAddress.Parse("127.0.0.1"),port); 
    //该处如果建立连接错误的话,将不执行下面的代码.. 
    portSum ++; 
    lindex = portList.Items.Add(port.ToString() + "端口开放"); 
    portList.SelectedIndex=lindex; 
    Stream sm = tcp.GetStream(); 
    sm.Write(Encoding.Default.GetBytes(tMsg.ToCharArray()),0,tMsg.Length); 
    StreamReader sr = new StreamReader(tcp.GetStream(),Encoding.Default); 
    getData = sr.ReadLine(); 
    if(lindex!=0&&getData.Length!=0) 

    tMsg = /*" +-" +*/ port.ToString() + "端口数据:"+getData.ToString(); 
    eindex = portList.Items.Add(tMsg); //插入一条信息记录 
    portList.Items.Insert(lindex+1,tMsg); 
    portList.Items.RemoveAt(eindex); 

    sr.Close(); 
    sm.Close(); 
    tcp.Close(); 

    catch 

    //显示坏死的端口 
    if(showdie.Checked==true) 

    portList.Items.Add(port.ToString()+"端口无法连接,回传数据为空"); 


    finally 

    Thread.Sleep(0); 
    if(this.myLogList!=null)
    this.myLogList.listBox1.Items.Add("结束线程:"+port.ToString()); 
    asyncOpsAreDone.Close(); 
    //statusBar1.Text = "端口总计:"+portSum.ToString() ; 
    if(connState==((Int32)EndNum.Value-(Int32)StartNum.Value)) 

    this.ScanButton.Text = "&Scan"; 


    }
    } private void button1_Click(object sender, System.EventArgs e)
    {
    this.myLogList = new MyListenForm.Form2(); // 
    // myLogList
    this.myLogList.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.myLogList.ClientSize = new System.Drawing.Size(432, 280);

    this.myLogList.MaximizeBox = false;
    this.myLogList.Name = "Form2";
    this.myLogList.Text = "扫描日志";
    this.myLogList.Visible = false;
    this.myLogList.Location=this.Location+(new Size(0,280));

    this.myLogList.StartPosition=System.Windows.Forms.FormStartPosition.Manual;
    this.myLogList.Show();
    //this.
    button1.Enabled=false;
    } private void Form1_LocationChanged(object sender, System.EventArgs e)
    {
    if(this.checkBox1.Checked==true)
    {
    if(Math.Abs(this.Location.X+this.Size.Width-this.myLogList.Location.X)<50)
    this.myLogList.Location=this.Location+(new Size(this.Size.Width,0));
    if(Math.Abs(this.Location.Y+this.Size.Height-this.myLogList.Location.Y)<50)
    this.myLogList.Location=this.Location+(new Size(0,this.Size.Height));
    }
    /*if(this.checkBox1.Checked==true)
    {
    //MessageBox.Show(this.Location.ToString()+this.myLogList.Location.ToString()+this.Size.ToString());
    if(this.Location.X==(this.myLogList.Location.X+this.Size.Width))
    {

    this.myLogList.Location=this.Location+(new Size(this.Size.Width,0));
    }
    if(this.Location==(this.myLogList.Location+(new Size(0,this.Size.Height))))
    this.myLogList.Location=this.Location+(new Size(0,this.Size.Height));
    }*/
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    this.myLogList.Location=this.Location+(new Size(0,this.Size.Height));
    } /*public static bool ButtonEnable
    {
    set
    {
    button1.Enabled=value;
    }
    }*/

    }
    }
      

  2.   

    楼上的,我问的是com端口,不是tcp端口。您答非所问了。
      

  3.   

    Windows下,注册表
    HKEY_LOCALE_MACHINE\Hardware\Devicemap\Serialcomm自己看吧