Beej's Guide Network to Programming series serialization 07

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.