Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. IP List

IP List

Scheduled Pinned Locked Moved C#
help
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krieg38
    wrote on last edited by
    #1

    Could someone give me a tip or help me make some code wich list all the ip addresses betwen to givel values. I need this for a host scaner and I spent a whole nigth working on it but I wrote only useless code. Thanks

    L K J 3 Replies Last reply
    0
    • K krieg38

      Could someone give me a tip or help me make some code wich list all the ip addresses betwen to givel values. I need this for a host scaner and I spent a whole nigth working on it but I wrote only useless code. Thanks

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      What is the problem you are having?

      xacc.ide-0.1.2.12
      Now with more features and one less bug!

      1 Reply Last reply
      0
      • K krieg38

        Could someone give me a tip or help me make some code wich list all the ip addresses betwen to givel values. I need this for a host scaner and I spent a whole nigth working on it but I wrote only useless code. Thanks

        K Offline
        K Offline
        krieg38
        wrote on last edited by
        #3

        For an example if I have 2 Ip address 127.0.0.1 and 127.0.2.1 I want to generate 127.0.0.1,127.0.0.2,127.0.0.3.......127.0.0.255....127.0.2.1 Do you understand now. Or if there is a link about this subject that woulb be helpful

        D 1 Reply Last reply
        0
        • K krieg38

          For an example if I have 2 Ip address 127.0.0.1 and 127.0.2.1 I want to generate 127.0.0.1,127.0.0.2,127.0.0.3.......127.0.0.255....127.0.2.1 Do you understand now. Or if there is a link about this subject that woulb be helpful

          D Offline
          D Offline
          Dario Solera
          wrote on last edited by
          #4

          Since an IP address is a 32 bit unsigned integer, you can convert the IPs into "flat" numbers, then generate all the addresses you want, then convert them into a "classic" IP address. For example:

          string[] startAddr = "127.0.0.1".Split('.');
          string[] endAddr = "127.2.0.1".Split('.');

          uint start = uint.Parse(startAddr[0]) +
            uint.Parse(startAddr[1]) * 256 +
            uint.Parse(startAddr[2]) * 256 * 256 +
            uint.Parse(startAddr[3]) * 256 * 256 * 256;
          uint end = ... // The same

          uint[] addr = new uint[end - start];
          for(int i = 0; i < addr.Length; i++) {
            addr[i] = start + i;
          }

          string[] result = new string[addr.Lenght];
          string tmp = "";
          for(int i = 0; i < result.Lenght; i++) {
             result[i] = "";
             result[i] += ((uint)(addr[i] % (256 * 256 * 256 * 256))).ToString() + ".";
             result[i] += ((uint)(addr[i] % (256 * 256 * 256))).ToString() + ".";
             result[i] += ((uint)(addr[i] % (256 * 256))).ToString() + ".";
             result[i] += ((uint)(addr[i] % (256))).ToString();
          }

          I don't know if this works (I didn't try it), but this is my idea. ;) EDIT: IT DOESN'T WORK AT ALL! ___________________________________ Tozzi is right: Gaia is getting rid of us. My Blog [ITA] -- modified at 4:59 Wednesday 15th February, 2006

          1 Reply Last reply
          0
          • K krieg38

            Could someone give me a tip or help me make some code wich list all the ip addresses betwen to givel values. I need this for a host scaner and I spent a whole nigth working on it but I wrote only useless code. Thanks

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            I had a go at this for you, and got it to work, but there were plenty of weirdnesses! IPAddress has a constructor that looks like: IPAddress(byte[] address) Which kept throwing an exception when passed the return from IPAddress.GetAddressBytes - this is weird as all the docs say it should work. There is a weird limitation that requires the byte array to have a length og 16 but II ensured this.... So anyway, IPAddress.Parse was working so a quick method such as the following gets round the issue. private IPAddress IPFromBytes(byte[] bytes) { return IPAddress.Parse( String.Format("{0}.{1}.{2}.{3}",bytes[0],bytes[1],bytes[2],bytes[3]) ); } Now, to solve this problem I started by thinking about how to incremement an IPAddress, and I came up with this: Loop from the last digit to the first if the current number is 255 set it to 0 otherwise incremement the number and break out the loop this should cause the sequence to go 127.0.0.1 127.0.0.2 ...... 127.0.0.254 127.0.0.255 127.0.1.0 127.0.1.1 Here is the incremement code: private byte[] Incremement(byte[] current) { for(int i=3;i>-1;i--) { if(current[i] == 255) { current[i] = 0; } else { current[i]++; break; } } return current; } Finally, the get range method must do the following capture start and end IP's loop while not at end capture current IP into a list incremement IP The only hard part there is "loop while not at end", as weve already written the incremement and capturing to a list is childs play. private System.Net.IPAddress [] GetRange(IPAddress start, IPAddress end) { byte[] startBytes = start.GetAddressBytes(); byte[] endBytes = end.GetAddressBytes(); byte[] currentBytes = startBytes; ArrayList list = new ArrayList(); while( currentBytes[0] < endBytes[0] || currentBytes[1] < endBytes[1] || currentBytes[2] < endBytes[2] || currentBytes[3] < endBytes[3]) { list.Add( IPFromBytes(currentBytes) ); currentBytes = this.Incremement(currentBytes); } return (IPAddress[])list.ToArray(typeof(IPAddress)); } Hope it helps, and please rememeber not to be doing anything malicious with port scanners!

            K 1 Reply Last reply
            0
            • J J4amieC

              I had a go at this for you, and got it to work, but there were plenty of weirdnesses! IPAddress has a constructor that looks like: IPAddress(byte[] address) Which kept throwing an exception when passed the return from IPAddress.GetAddressBytes - this is weird as all the docs say it should work. There is a weird limitation that requires the byte array to have a length og 16 but II ensured this.... So anyway, IPAddress.Parse was working so a quick method such as the following gets round the issue. private IPAddress IPFromBytes(byte[] bytes) { return IPAddress.Parse( String.Format("{0}.{1}.{2}.{3}",bytes[0],bytes[1],bytes[2],bytes[3]) ); } Now, to solve this problem I started by thinking about how to incremement an IPAddress, and I came up with this: Loop from the last digit to the first if the current number is 255 set it to 0 otherwise incremement the number and break out the loop this should cause the sequence to go 127.0.0.1 127.0.0.2 ...... 127.0.0.254 127.0.0.255 127.0.1.0 127.0.1.1 Here is the incremement code: private byte[] Incremement(byte[] current) { for(int i=3;i>-1;i--) { if(current[i] == 255) { current[i] = 0; } else { current[i]++; break; } } return current; } Finally, the get range method must do the following capture start and end IP's loop while not at end capture current IP into a list incremement IP The only hard part there is "loop while not at end", as weve already written the incremement and capturing to a list is childs play. private System.Net.IPAddress [] GetRange(IPAddress start, IPAddress end) { byte[] startBytes = start.GetAddressBytes(); byte[] endBytes = end.GetAddressBytes(); byte[] currentBytes = startBytes; ArrayList list = new ArrayList(); while( currentBytes[0] < endBytes[0] || currentBytes[1] < endBytes[1] || currentBytes[2] < endBytes[2] || currentBytes[3] < endBytes[3]) { list.Add( IPFromBytes(currentBytes) ); currentBytes = this.Incremement(currentBytes); } return (IPAddress[])list.ToArray(typeof(IPAddress)); } Hope it helps, and please rememeber not to be doing anything malicious with port scanners!

              K Offline
              K Offline
              krieg38
              wrote on last edited by
              #6

              Thx, i will see if I manage to solve my problem. And don`t be afraid I don`t want to make port scanners. I try to make an utility that detects all PC from a Lan by pinging them :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups