遍歷區域網IP和Hostname的方法

努力的藍精靈發表於2013-10-04


void Find( ) // 
    {
      Add( "-- >>> >> >>> Found station <<< << <<< -- " );
      int lastF = ipFrom.Text.LastIndexOf(".");
      int lastT = ipTo.Text.LastIndexOf(".");
      string frm = ipFrom.Text.Substring(lastF+1);
      string tto = ipTo.Text.Substring(lastT+1);
      int result = 0;
      System.Diagnostics.Debug.WriteLine( frm + " " + tto);
      for( int i = int.Parse( frm); i <= int.Parse(tto );i++)
      {
        try
        {
          string address = ipTo.Text.Substring(0,lastT+1);
          System.Diagnostics.Debug.WriteLine(ipTo.Text.Substring(0,lastT+1)+i);
          IPHostEntry he = Dns.GetHostByAddress( address+i);
          Add( he.HostName );
          result += 1;
        }


        catch( SocketException )
        {
          // in cazul unei erori
        }
        catch( Exception )
        {
          // previne bloacarea programului
        }
      }


    }

相關文章