So, I'm learning about sockets and reading everything in sight. I started digging into the developer's handbook and after an entire section about the perils of byte ordering in socket connections, I was presented with this little gem:
Of course, after I read the manpage for htonl, I knew that it took an address and stored it in network order - yippee. But it took a minute for me to realize what:
was for. Seriously - 132.163.96.3 (his ip was different, but same effect)? Surely, there's an easier way to do network address to integer conversions... So, I went looking, and found:
Hmm... seems reasonable (inet_addr returns an address in network order). But, this network coding is soooo new to me. What's the scoop? Is the example as obtuse as it seems or is it somehow a socket programming best practice?
Code:
sa.sin_addr.s_addr = htonl((((((132 << 8) | 163) << 8) | 96) << 8) | 3);
Of course, after I read the manpage for htonl, I knew that it took an address and stored it in network order - yippee. But it took a minute for me to realize what:
Code:
(((((132 << 8) | 163) << 8) | 96) << 8) | 3
was for. Seriously - 132.163.96.3 (his ip was different, but same effect)? Surely, there's an easier way to do network address to integer conversions... So, I went looking, and found:
Code:
sa.sin_addr.s_addr = inet_addr("132.163.96.3");
Hmm... seems reasonable (inet_addr returns an address in network order). But, this network coding is soooo new to me. What's the scoop? Is the example as obtuse as it seems or is it somehow a socket programming best practice?