Network programming for PyS60 (V)

by Marcelo Barros

Before some more practicing, it is time to finish the last post about IP addressing. One important concept is missing: network masks.

After CIDR adoption, the network mask became essential for splitting the IP address in network ID and host ID. This way, the default masks for classes A, B and C could be increased (typical case for sub-networking) or even decreased (common in routers for saving routing tables entries).

In fact, the network mask is built putting bits in 1 from left to right in the part corresponding to the network ID and leaving the remaining bits in 0 (host ID part).

For instance, consider the class A network address 10.0.0.0. We will use the network mask 255.255.255.0. This mask has 1 in the first 24 bits (three consecutive bytes with 255) creating the network ID “10.0.0” and leaving one additional byte for host ID. The CIDR notation for this mask is “/24”, an abbreviated expression to say the amount of bits set.

Different machines in this network will have distinct host IDs, just changing the bit values in the host ID. Two host IDs are reserved: the first one (all bits in host ID reset) and the last one (all bits in host ID set). As you have already guessed this first address is the network address but the last one is new in our discussion. It is called broadcast address and it is reserved for sending messages for all machines in this subnet.

In summary:


Network: 10.0.0.0
Mask: 255.255.255.0 or /24 (CIDR notation)
IP addresses range: 10.0.0.1 to 10.0.0.254 (to be used in your machines)
Broadcast: 10.0.0.255

Let’s examine a more complex example. Suppose you received the following network/mask from your ISP (Internet Service Provider):


Network: 200.201.145.128
Mask: 255.255.255.192 or /26 (CIDR notation)

Quite strange no ? In fact, no. I will show you.

Put your attention in the mask. The first two significant bits in the last byte of the mask are set (192 is 11000000 in binary – we will use suffixes ‘d’ and ‘b’ to denote bases). This means that the first two significant bits in 128 ( 10000000b) belongs to the network ID and only the last six bits can be used to host ID.

So, when all bits in host ID are reset (10000000 b or 128d) we have the network address and when they are set we obtain the broadcast address (10111111 b or 191d).

In summary:


Network: 200.201.145.128
Mask: 255.255.255.192 or /26 (CIDR notation)
Broadcast: 200.201.145.191
IP addresses range: 200.201.145.129 to 200.201.145.190 (to be used in your machines)

How about use this knowledge for creating a adhoc connection between two smartphones ? Really simple. Just select one reserved network, define your mask, choose two different host IDs for IPs and set your security parameters/protocols. The connection setup will require a default gateway. If you do not want to access any network/computers outside your ahdoc network, use your own IP as default gateway. In fact, default gateway is related to routing, defining the path to reach other networks.

But this is subject for another post …

Publicado pelo Wordmobi

Related posts:

  1. Network programming for PyS60 (IV) Network programming is innocuous if you don't have at least...
  2. Network programming for PyS60 (XIII) In our last post we talked about multicast, a special...
  3. Network programming for PyS60 (XI) We have already talked about IP addressing in the post...
  4. Network programming for PyS60 (III) As I promised, today we will start with some programming,...
  5. Network programming for PyS60 (XII) Until now we have used only TCP in our examples...

Related posts brought to you by Yet Another Related Posts Plugin.