标题:线程扫描IP地址段 错误:索引超出了数组界限。
只看楼主
f2831890
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:115
专家分:107
注 册:2009-11-25
结帖率:96.77%
已结贴  问题点数:5 回复次数:2 
线程扫描IP地址段 错误:索引超出了数组界限。
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using using using System.Collections;
using namespace 使用线程扫描计算机
{
    public delegate void UpdateList(string sIP, string sHostName);

    public partial class Form1 : Form
    {
        private System.DateTime StartTime;

        public Form1()
        {
            InitializeComponent();
            Form1.CheckForIllegalCrossThreadCalls = false;
        }
        


        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            StartTime = DateTime.Now;
            string mask = numericUpDown1.Value.ToString() + "." + numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() + ".";
            int Min = (int)numericUpDown4.Value;
            int Max = (int)numericUpDown5.Value;
            if (Min > Max)
            {
                MessageBox.Show("输入地址取件不合法", "错误");
                return;
            }
            int _ThreadNum = Max - Min + 1;
            Thread[] mythread = new Thread[_ThreadNum];
            progressBar1.Minimum = Min;
            progressBar1.Maximum = Max;
            int i;
            for (i = Min; i <= Max; i++)
            {
                int k = Max-1;
                ping HostPing = new ping();
                HostPing.ip = mask + i.ToString();
                HostPing.ul = new UpdateList(UPdateMyList);
                mythread[k] = new Thread(new ThreadStart(HostPing.scan));
                mythread[k].Start();
            }
        }

        public class ping
        {
            public UpdateList ul;
            public string ip;
            public string HostName;
            public void scan()
            {
                IPAddress myIP = IPAddress.Parse(ip);
                try
                {
                    IPHostEntry myHost = Dns.GetHostEntry(myIP);
                    HostName = myHost.HostName.ToString();
                }
                catch
                {
                    HostName = "";
                }
                if (HostName == "")
                    HostName = "主机没有响应!";
                if (ul != null)
                    ul(ip, HostName);
            }
        }

        void UPdateMyList(string sIP, string sHostName)
        {
            lock (listBox1)
            {
               listBox1.Items.Add(sIP + " " + sHostName);

                if (progressBar1.Value != progressBar1.Maximum)
                {
                    progressBar1.Value++;
                }
                if (progressBar1.Value == progressBar1.Maximum)
                {
                    MessageBox.Show("成功完成检测!", "提示");
                    DateTime EndTime = DateTime.Now;
                    TimeSpan ts = EndTime - StartTime;
                    label4.Text = ts.Seconds.ToString() + "";
                    progressBar1.Value = progressBar1.Minimum;
                }
            }
        }
    }
}



mythread[k] = new Thread(new ThreadStart(HostPing.scan));
                mythread[k].Start();                       错误提示:索引超出了数组界限。


该怎么解决。。。。。













搜索更多相关主题的帖子: IP地址 
2011-07-15 13:54
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
得分:0 
检查一下k的取值范围。
2011-07-15 15:28
jiajiajing6
Rank: 1
等 级:新手上路
帖 子:8
专家分:6
注 册:2010-10-16
得分:5 
for (i = Min; i <= Max; i++)
            {
                int k = Max-1;


这里改为  int k= i- Min;




2011-07-15 15:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-345550-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055302 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved