Socket functions

From Ilianko


Byte Order Conversion

  • htons() host to network short
  • htonl() host to network long
  • ntohs() network to host short
  • ntohl() network to host long

IP address conversion

struct sockaddr_in sa; // IPv4
struct sockaddr_in6 sa6; // IPv6

inet_pton(AF_INET, "192.0.2.1", &(sa.sin_addr)); // IPv4
inet_pton(AF_INET6, "2001:db8:63b3:1::3490", &(sa6.sin6_addr)); // IPv6