Calculating subnet from an IP

Kevin

Well-Known Member
Licensed User
Longtime User
Does anyone have working code on how to calculate a subnet from an IP address? I've seen a couple posts here but it's not helping me much. Normally I don't mind figuring things out myself but this time I'm just flat out going to ask for someone to hold my hand! :D

The deal is this: My app scans all possible addresses for satellite receivers. On most typical networks this happens within a couple minutes. Basically I do this:

If the IP address of the device is 192.168.1.110, then I try sending an HTTP request to 192.168.1-254 (not including 110). However, a customer recently emailed to tell me he only has around 32 possible IP addresses on his network (not 254) and my scan takes 30 whopping minutes! Not good at all. So he has been talking about subnet masks, and subnets, etc, to determine the actual range of possible addresses on the network.

The only problem is that it is all over my head and I don't understand any of it.

So if anyone has some working code for determining a valid range of IP addresses on a network initially derived only from a single known IP on the network (i.e. the IP address of the Android device on the network) I'd appreciate if you can help.

Apparently one can get the subnet with a combination of an actual IP address and the default subnet mask for that network type (A, B, or C). That is all I know. :eek:

Thanks!
 

Penko

Active Member
Licensed User
Longtime User
I can't really answer your question but want to ask you two more. Are you checking these IP addresses sequentially or simultaneously? Are you using HTTPUtils2 to simplify your job? According to documentation, there is no limit of simultaneous connections anymore. You can use proper job names to determine which connection has been successful and which one failed. That may be much faster and not 30 minutes. Check the documentation for timeout capabilities because it is redundant to wait longer than necessary.

Sent from my HTC Desire using Tapatalk 2
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Kevin,

You mentioned the network mask, that's indeed what defines a subnet and how it can access others.

your 192.168.1.x range will be a 255.255.255.0 netmask.
Which also known as 192.168.1.0/24 since you have 24 bits turned on in the mask.

the 0 defines that you can use all bits of the last octet.

if you put a 1 on bit 6 & 7 in the mask it will mean that the first possible address will be 192.168.1.64 (or a /26)

so it's just a matter of counting bits or substracting the mask values from 255

in your case an IP address is not enough, you'll need the IP address and mask to be able to calculate the valid subnet.
 
Last edited:
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
Thanks for the replies. I'm temporarily taking a detour on this. The customer tried a competing app that scans 254 possible addresses as well (even though in his network, there are only 32). However, the competing app finishes in a couple minutes (like my app does on my own network).

I now suspect that my timeout is way too high. I had raised it a while back because I saw no ill side effects from it on my network but I am thinking that on certain uncommon networks like his, this high timeout (65 seconds!) is causing major slowdowns.

Sometimes these satellite receivers can be pokey to respond (granted, not 65 seconds though)... I'm not sure what an ideal timeout would be over a local network. 5 seconds? 10 seconds?

BTW: I use the original HTTPUtils and add all possible URLs to a download list.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
Does your receiver support DLNA?

Unless I am wrong I think DLNA-enabled devices actually make some broadcast on the network making them easier to find. DLNA/uPNP clients normaly send out a broadcast signal that can reach the clients of the local subnet. Many multimedia-devices these days are handling DLNA.

I might have a look at it at a later stage.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Why not use some kind of (fake) threading?

Scaning it in block of 32 or 64 IP address, that would gain a lot of time.

Ofcource if your http response is stuck for 60 seconds you'll still waste a minute but for the entire network then.

I only used the http lib so far to communicate via http with a webserver, will have a look at what you're using there.
 
Upvote 0
Top