This is a rather basic problem with which, to my surprise, I've had a problem today.
It looks to me like inet_pton and inet_ntoa are reversing the bytes of the IP address they're given:
inet_pton(AF_INET, "192.168.0.1", &IP);
01.00.A8.C0 = 220.127.116.11
IP = 0x7F000001;
struct in_addr ia;
ia.S_un.S_addr = IP;
Endianness is the reason.
The whole point of these functions is not to produce a "readable" integer, but to set a 32-bit quantity that is ready to be shipped out on the wire. IPv4 requires big-endian ordering, so I would wager that if you did
printf("%02X\n", ((char *)&IP)));, you'd get