Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# socket Networking
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 4-Jul-12 0:28am
Comments
Zoltán Zörgő at 15-Jul-12 3:27am
   
Any progress?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
asdf9009 at 12-Jul-12 2:43am
   
Thanks for the link!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

See this code:
 
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);
		}
	}
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 12 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100