4. from IPv4 to IPv6
But I want to know how to change from current code to work on IPv6! Tell me!
Good ~~
If we say so much, we just talk about him.
1) First, use getaddrinfo () to obtain the struct sockeaddr information to replace our encapsulation. This will make your program code irrelevant to the IP version for our subsequent steps.
2) use other functions in any hard-coded place.
3) Change AF_INET to AF_INET6.
4) Change PF_INET to PF_INET6 www.2cto.com.
5) change the value of INADDR_ANY to in6addr_any.
Struct sockaddr_in sa;
Struct sockaddr_in6 sa6;
Sa. sin_addr.s_addr = INADDR_ANY; // use my IPv4 address
Sa6.sin6 _ addr = in6addr_any; // use my IPv6 address
When we declare struct in6_addr, we can also use IN6ADDR_ANY_INIT to initialize it. As shown below:
Struct in6_addr ia6 = IN6ADDR_ANY_INIT;
6) replace struct sockaddr_in6 with struct sockaddr_in. Because the sin6_zero field is not found in the previous version.
7) replace struct in_addr with structin6_addr.
8) replace inet_aton () and inet_addr () with inet_ton ();
9) replace inet_ntoa () with inet_ntoa ();
10) Use getaddrinfo () to replace gethostbyname ();
11) Use getnameinfo () to replace gethostbyaddr ();
Although gethostbyaddr () can also work on IPv6.
12) replace INADDR_BROADCAST with IPv6 multicast (multicast.
That's it!
From the column xiaobin_HLJ80