It's an endian problem
In your system the inet_addr("192.168.1.1")
returns the ip address presented in 4 octets in this way:
0x01 | 0x01 | 0xA8 | 0xC0
// 1 . 1 . 168 . 192
the inet_addr("192.168.1.2")
returns
0x02 | 0x01 | 0xA8 | 0xC0
// 2 . 1 . 168 . 192
The difference inet_addr("192.168.1.2") - inet_addr("192.168.1.2")
will be:
0x0201A8C0 - 0x0101A8C0 = 0x1000000 // in decimal is 16777216
Use htonl()
function for each inet_addr()
inorder to avoid the endian issues
So htonl()
of inet_addr("192.168.1.2")
will return
0xC0 | 0xA8 | 0x01 | 0x02
// 192 . 168 . 1 . 2
And htonl()
of inet_addr("192.168.1.1")
returns
0xC0 | 0xA8 | 0x01 | 0x01
// 192 . 168 . 1 . 1
And then the difference will be equal to 1
addr1.sin_addr.s_addr = htonl(inet_addr("192.168.1.1"));
addr2.sin_addr.s_addr = htonl(inet_addr("192.168.1.2"));
addr3.sin_addr.s_addr = htonl(inet_addr("192.168.1.3"));
addr4.sin_addr.s_addr = htonl(inet_addr("192.168.1.255"));
addr5.sin_addr.s_addr = htonl(inet_addr("192.168.2.1"));