Click here to Skip to main content
15,885,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The articles i found in CodeProject is just basically scan the network and list out whatever is existed in the network.
Now i want to limit the scanning by adding "Starting of IP address", and "Ending of IP address".

Any idea how? or just a "if" or "for" loop to limit the searching range?
Posted
Comments
Zoltán Zörgő 15-Jul-12 3:27am    
Any progress?

 
Share this answer
 
Comments
asdf9009 12-Jul-12 2:43am    
Thanks for the link!
See this code:

C#
using System;
using System.Net;
using System.Linq;
using System.Collections;
using System.Runtime.InteropServices;

namespace devlist
{
	public class IPEnumeration: IEnumerable
	{
		private string startAddress;
		private string endAddress;
		
		internal static Int64 AddressToInt(IPAddress addr)
	    {
			byte[] addressBits = addr.GetAddressBytes();
			
	        Int64 retval = 0;
	        for (int i = 0; i < addressBits.Length; i++)
	        {
	            retval = (retval << 8) + (int)addressBits[i];
	        }
	
	        return retval;
	    }		
		
		internal static Int64 AddressToInt(string addr)
	    {
			return AddressToInt(IPAddress.Parse(addr));
	    }

	    internal static IPAddress IntToAddress(Int64 addr)
	    {
	    	return IPAddress.Parse(addr.ToString());
	    }	        
	    
	    
	    public IPEnumeration(string startAddress, string endAddress)
	    {
	    	this.startAddress = startAddress;
	    	this.endAddress = endAddress;
	    }
	    
	    IEnumerator IEnumerable.GetEnumerator()
	    {
	       return (IEnumerator) GetEnumerator();
	    }
	
	    public IPEnumerator GetEnumerator()
	    {
	        return new IPEnumerator(startAddress, endAddress);
	    }    
	    
	}
	
	public class IPEnumerator: IEnumerator
	{
		private string startAddress;
		private string endAddress;
		private Int64 currentIP;
		private Int64 endIP;
		
		public IPEnumerator(string startAddress, string endAddress)
		{
			this.startAddress = startAddress;
	    	this.endAddress = endAddress;	
	    	
	    	currentIP = IPEnumeration.AddressToInt(startAddress);
	    	endIP = IPEnumeration.AddressToInt(endAddress);
		}
		
		public bool MoveNext()
	    {
	        currentIP++;
	        return (currentIP <= endIP);
	    }
	
	    public void Reset()
	    {
	        currentIP = IPEnumeration.AddressToInt(startAddress);
	    }
	
	    object IEnumerator.Current
	    {
	        get
	        {
	            return Current;
	        }
	    }
	
	    public IPAddress Current
	    {
	        get
	        {
	            try
	            {
	            	return IPEnumeration.IntToAddress(currentIP);
	            }
	            catch (IndexOutOfRangeException)
	            {
	                throw new InvalidOperationException();
	            }
	        }
	    }
	}
	
    public static class IPHelper
    {
    	[DllImport("iphlpapi.dll", ExactSpelling=true)]
    	public static extern int SendARP( int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);
    	
    	public static string getMAC(IPAddress address)
    	{
    		int intAddress = BitConverter.ToInt32(address.GetAddressBytes(), 0);

			byte[] macAddr = new byte[6];
			uint macAddrLen = (uint) macAddr.Length;
			if (SendARP(intAddress, 0, macAddr, ref macAddrLen) != 0)
			    return "(NO ARP result)";
			
			string[] str = new string[(int)macAddrLen];
			for (int i = 0; i < macAddrLen; i++)
			    str[i] = macAddr[i].ToString("x2");
			
			return string.Join(":", str);
    	}
    }
    
	class Program
	{
		public static void Main(string[] args)
		{
			
			foreach(IPAddress addr in new IPEnumeration("172.25.216.10","172.25.216.20"))
	        {
				Console.WriteLine("{0}\t\t{1}",addr.ToString(), IPHelper.getMAC(addr));
	        }
			
			Console.ReadKey(true);
		}
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900