C++ Socket Programmin swali for the C/C++ gurus

Jambo skunks! Is there any clear cut advantage in using "inet_pton()" over assigning an IP address to "*sockaddr_in.sin_addr.s_addr*" *Yaani:-* *inet_pton(AF_INET,host.c_str(),&m_addr.sin_addr);* *VS.* *sockaddr_in**. sin_addr.s_addr = inet_addr(host.c_str());* * * * * * * _______________________________________________ *In programming, as in everything else, to be in error is to be reborn.** * _______________________________________________ * *

Appart from being able to use IPv6 ofcourse ← I forgot to mention this _______________________________________________ *In programming, as in everything else, to be in error is to be reborn.** * _______________________________________________ * * 2012/9/12 James Nzomo <kazikubwa@gmail.com>
Jambo skunks!
Is there any clear cut advantage in using "inet_pton()" over assigning an IP address to "*sockaddr_in.sin_addr.s_addr*"
*Yaani:-* *inet_pton(AF_INET,host.c_str(),&m_addr.sin_addr);* *VS.* *sockaddr_in* *.sin_addr.s_addr = inet_addr(host.c_str());* * * * * * * _______________________________________________
*In programming, as in everything else, to be in error is to be reborn.** * _______________________________________________ *
*

Hi, the problem is when you use inet_addr() to assign the address to the sin_addr struct. inet_addr() can return an error of -1 (INADDR_NONE) for invalid input.. unfortunately -1 is actually a valid address (255.255.255.255). inet_pton() handles this situation much better and of course the support for ipv6 Steve
Jambo skunks!
Is there any clear cut advantage in using "inet_pton()" over assigning an IP address to "*sockaddr_in.sin_addr.s_addr*"
*Yaani:-* *inet_pton(AF_INET,host.c_str(),&m_addr.sin_addr);* *VS.* *sockaddr_in**. sin_addr.s_addr = inet_addr(host.c_str());* * * * * * * _______________________________________________
*In programming, as in everything else, to be in error is to be reborn.** * _______________________________________________ *
* _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

I'm writing a cross platform { read posix sockets & winsock } c++ wrapper for an app i'm making. (yes i know there are some on the net.....but i'm doing it from scratch for the adventure) The problem is *inet_pton() *is missing on windows. M$ decided to provide a alternative but it's only supported on vista and beyond. Considering that IPv4 is more widespread that v6 and that the "return -1 bug" depends on the back end in use, if you were to code the same wrapper and wanted to make it as seamless as possible (featurewise), would you skip it in favour of *inet_addr()*? _______________________________________________ *In programming, as in everything else, to be in error is to be reborn.** * _______________________________________________

You'd probably want to pre-validate the input into inet_addr() to handle the INADDR_NONE bug. Seeing it takes a human readable ip format as ipv4 you can easily use regular expressions to validate the format before passing it into inet_addr(). So if I had no otherwise but to use inet_addr()I would do it like that... Steve
I'm writing a cross platform { read posix sockets & winsock } c++ wrapper for an app i'm making. (yes i know there are some on the net.....but i'm doing it from scratch for the adventure)
The problem is *inet_pton() *is missing on windows. M$ decided to provide a alternative but it's only supported on vista and beyond.
Considering that IPv4 is more widespread that v6 and that the "return -1 bug" depends on the back end in use, if you were to code the same wrapper and wanted to make it as seamless as possible (featurewise), would you skip it in favour of *inet_addr()*?
_______________________________________________
*In programming, as in everything else, to be in error is to be reborn.** * _______________________________________________ _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

Thank you sir. That's the reassurance i was looking/hoping for. _______________________________________________ *In programming, as in everything else, to be in error is to be reborn.** * _______________________________________________
participants (2)
-
James Nzomo
-
Steve Richard