2 ms·
Most programs would simply use bind(), instead of having a more complicated option parsing and condition around bind()/bind_unused(): sockaddr_in sockaddr
by Joker_vD 6y ago
Most programs would simply use bind(), instead of having a more complicated option parsing and condition around bind()/bind_unused():
sockaddr_in sockaddr = { 0 };
sockaddr.sin_port = HARDCODED_PORT;
// other fields defaulted...
// option parsing and filling sockaddr structure...
if (has_port_number && flag_use_random_port) {
error(...);
exit(1);
}
if (has_port_number) {
bind(s, sockaddr);
} else {
bind_unused_port(s, sockaddr);
}
vs.
sockaddr_in sockaddr = { 0 };
sockaddr.sin_port = HARDCODED_PORT;
// other fields defaulted...
// option parsing and filling sockaddr structure...
bind(s, sockaddr);
Yeah, people would just write the second version. And mind you, the sockaddr argument for bind_unused() still would have a local address to bind to, so what exactly is gained? Difference between 65535 or 65536 ports available doesn't warrant complicating API in such a manner, IMO.