Exemplo n.º 1
0
        /// <summary>
        /// 开始扫描
        /// </summary>
        public void StartScan()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object stateObj)
            {
                //事件开始
                if (StartEvent != null)
                {
                    StartEvent(this, new EventArgs());
                }

                while (ScanQueues.Count > 0)
                {
                    try
                    {
                        //取IP地址
                        IPEndPoint ipe = null;
                        ScanQueues.TryDequeue(out ipe);

                        //发送查询指令
                        if (ipe != null)
                        {
                            byte[] queryCmd = Encoding.UTF8.GetBytes(CommandConst.QUERY_ROBOT_STATUS);
                            UdpClient.UdpClient.Send(queryCmd, queryCmd.Length, ipe);

                            //投递事件
                            if (ProgressEvent != null)
                            {
                                ProgressEventArgs pea = new ProgressEventArgs();
                                pea.Remote            = ipe;
                                ProgressEvent(this, pea);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine(ex.ToString());
                    }

                    //结束事件
                    if (ScanQueues.Count == 0)
                    {
                        if (StopEvent != null)
                        {
                            StopEvent(this, new EventArgs());
                        }
                    }

                    try
                    {
                        Thread.Sleep(5);
                    }
                    catch (Exception ex) { }
                }
            }));
        }
Exemplo n.º 2
0
 /// <summary>
 /// 初始化扫描队列(仅支持IPv4,不支持跨网段)
 /// </summary>
 /// <param name="ipStart">IP段开始,0.0.0.1</param>
 /// <param name="ipEnd">IP段结束,0.0.0.255</param>
 /// <param name="portStart">端口开始</param>
 /// <param name="portEnd">端口结束</param>
 public void InitQueues(string boardcastIpAddress, int portStart, int portEnd)
 {
     //生成端口
     for (int portNum = portStart; portNum <= portEnd; portNum++)
     {
         try
         {
             ScanQueues.Enqueue(new IPEndPoint(IPAddress.Parse(boardcastIpAddress), portNum));
         }
         catch (Exception ex) { }
     }
 }