UNIX Network Programming Volume 1, Third Edition [Electronic resources] : The Sockets Networking API نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

UNIX Network Programming Volume 1, Third Edition [Electronic resources] : The Sockets Networking API - نسخه متنی

Addison Wesley

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










11.10 getaddrinfo Function: Examples


We will now show some examples of getaddrinfo using a test program that lets us enter all the parameters: the hostname, service name, address family, socket type, and AI_CANONNAME and AI_PASSIVE flags. (We do not show this test program, as it is about 350 lines of uninteresting code. It is provided with the source code for the book, as described in the Preface.) The test program outputs information on the variable number of addrinfo structures that are returned, showing the arguments for a call to socket and the address in each socket address structure.

We first show the same example as in Figure 11.5.



freebsd %

testga -f inet -c -h freebsd4 -s domain
socket (AF_INET, SOCK_DGRAM, 17), ai_canonname = freebsd4.unpbook.com
address: 135.197.17.100:53
socket (AF_INET, SOCK_DGRAM, 17)
address: 172.24.37.94:53
socket (AF_INET, SOCK_STREAM, 6), ai_canonname = freebsd4.unpbook.com
address: 135.197.17.100:53
socket (AF_INET, SOCK_STREAM, 6)
address: 172.24.37.94:53



The -f inet option specifies the address family, -c says to return the canonical name, -h bsdi specifies the hostname, and -s domain specifies the service name.

The common client scenario is to specify the address family, socket type (the -t option), hostname, and service name. The following example shows this for a multihomed host with three IPv4 addresses:



freebsd %

testga -f inet -t stream -h gateway.tuc.noao.edu -s daytime
socket (AF_INET, SOCK_STREAM, 6)
address: 140.252.108.1:13
socket (AF_INET, SOCK_STREAM, 6)
address: 140.252.1.4:13
socket (AF_INET, SOCK_STREAM, 6)
address: 140.252.104.1:13



Next, we specify our host aix, which has both a AAAA record and an A record. We do not specify the address family, but we provide a service name of ftp, which is provided by TCP only.



freebsd %

testga -h aix -s ftp -t stream
socket (AF_INET6, SOCK_STREAM, 6)
address: [3ffe:b80:1f8d:2:204:acff:fe17:bf38]:21
socket (AF_INET, SOCK_STREAM, 6)
address: 192.168.42.2:21



Since we didn't specify the address family, and since we ran this example on a host that supports both IPv4 and IPv6, two structures are returned: one for IPv4 and one for IPv6.

Next, we specify the AI_PASSIVE flag (the -p option); we do not specify an address family or hostname (implying the wildcard address). We also specify a port number of 8888 and a stream socket.



freebsd %

testga -p -s 8888 -t stream
socket (AF_INET6, SOCK_STREAM, 6)
address: [: :]: 8888
socket (AF_INET, SOCK_STREAM, 6)
address: 0.0.0.0:8888



Two structures are returned. Since we ran this on a host that supports IPv6 and IPv4 without specifying an address family, getaddrinfo returns the IPv6 wildcard address and the IPv4 wildcard address. The IPv6 structure is returned before the IPv4 structure, because we will see in Chapter 12 that an IPv6 client or server on a dual-stack host can communicate with either IPv6 or IPv4 peers.


/ 450