c++ - How to convert string to IP address and vice versa -


How do I change a string ipAddress (struct in_addr) and vice versa? And how do I change an unsigned long IP address? Thanks

Use inet_ntop () and inet_pton () If you need another type around it, do not use inet_ntoa (), inet_aton () and as they disliked and do not support ipv6.

Here's a nice one with some examples.

  // IPv4 demo inet_ntop () and inet_pton () struct sockaddr_in sa; Four straw [INET_ADDRSTRLEN]; // Keep this IP address in: inet_pton (AF_INET, "192.0.2.33", & amp; (sa.sn_addr)); // Now get it back and print inet_ntop (AF_INET, & amp; (sa.sn_addr), str, INET_ADDRSTRLEN); Printf ("% s \ n", str); // print "192.0.2.33"    

Comments