2.3 2.3 g (Web hosting ratings) 17 if (hp->h_length != sizeof(sin6.sin6_addr))

2.3 2.3 g 17 if (hp->h_length != sizeof(sin6.sin6_addr)) { fprintf(stderr, invalid address sizen ); exit(1); /*NOTREACHED*/ } memset(&sin6, 0, sizeof(sin6)); sin6.sin6_family = AF_INET6; salen = sin6.sin6_len = sizeof(struct sockaddr_in6); memcpy(&sin6.sin6_addr, hp->h_addr, sizeof(sin6.sin6_addr)); sin6.sin6_port = htons(80); /* connect to the peer */ if (connect(s, (struct sockaddr *)&sin6, salen) 0) { perror( connect ); exit(1); } 2.3 Guidelines to Address-Family Independent Socket Programming So, how can we make our program address-family independent? This section enumerates important tips to be followed to achieve this goal. 2.3.1 Using sockaddrs for address representation To support IPv4/v6 dual stack from your program, you first need to be able to handle IPv4 and IPv6 addresses in your program. Traditionally, IPv4-only programs used struct in_addr to hold IPv4 addresses. However, since the structure does not contain an identification of address family, the data is not self-contained. /* * this example is IPv4-only, and we cannot identify address family * from the data itself. foo() cannot distinguish the address * family of the given address. * inet_addr(3) is not recommended due to the lack of failure handling. */ extern void foo(void *); struct in_addr in; if (inet_aton( 127.0.0.1″, &in) != 1) { fprintf(stderr, could not translate addressn ); exit(1); } foo(&in); Novice programmers even mistakenly use int or u_int32_t to hold IPv4 addresses. This is not a portable way, since int can be of a different size (e.g., 64 bits), and from a Chapter 2
Our company is website hosting provider which offers web hosting services for php, java, mysql, frontpage, dreamweaver and domain name registration. Check more about us on website hosting provider part.

Leave a Reply