Debian Codegraph @

upstream/debian-unstable/man3/getaddrinfo.3

raw
1 '\" t
2 .\" Copyright 2007-2008, Michael Kerrisk <[email protected]>
3 .\" Copyright 2006, Ulrich Drepper <[email protected]>
4 .\" Copyright, the authors of the Linux man-pages project
5 .\"
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\"
8 .\" FIXME . glibc's 2.9 NEWS file documents DCCP and UDP-lite support
9 .\" and is SCTP support now also there?
10 .\"
11 .TH getaddrinfo 3 2026-02-08 "Linux man-pages 6.18"
12 .SH NAME
13 getaddrinfo, freeaddrinfo, gai_strerror \- network address and
14 service translation
15 .SH LIBRARY
16 Standard C library
17 .RI ( libc ,\~ \-lc )
18 .SH SYNOPSIS
19 .nf
20 .B #include <sys/types.h>
21 .B #include <sys/socket.h>
22 .B #include <netdb.h>
23 .P
24 .BI "int getaddrinfo(const char *restrict " node ,
25 .BI " const char *restrict " service ,
26 .BI " const struct addrinfo *restrict " hints ,
27 .BI " struct addrinfo **restrict " res );
28 .P
29 .BI "void freeaddrinfo(struct addrinfo *" res );
30 .P
31 .BI "const char *gai_strerror(int " errcode );
32 .fi
33 .P
34 .RS -4
35 Feature Test Macro Requirements for glibc (see
36 .BR feature_test_macros (7)):
37 .RE
38 .P
39 .BR getaddrinfo (),
40 .BR freeaddrinfo (),
41 .BR gai_strerror ():
42 .nf
43 Since glibc 2.22:
44 _POSIX_C_SOURCE >= 200112L
45 glibc 2.21 and earlier:
46 _POSIX_C_SOURCE
47 .fi
48 .SH DESCRIPTION
49 Given
50 .I node
51 and
52 .IR service ,
53 which identify an Internet host and a service,
54 .BR getaddrinfo ()
55 returns one or more
56 .I addrinfo
57 structures, each of which contains an Internet address
58 that can be specified in a call to
59 .BR bind (2)
60 or
61 .BR connect (2).
62 The
63 .BR getaddrinfo ()
64 function combines the functionality provided by the
65 .\" .BR getipnodebyname (3),
66 .\" .BR getipnodebyaddr (3),
67 .BR gethostbyname (3)
68 and
69 .BR getservbyname (3)
70 functions into a single interface, but unlike the latter functions,
71 .BR getaddrinfo ()
72 is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
73 .P
74 The
75 .I addrinfo
76 structure used by
77 .BR getaddrinfo ()
78 contains the following fields:
79 .P
80 .in +4n
81 .EX
82 struct addrinfo {
83 int ai_flags;
84 int ai_family;
85 int ai_socktype;
86 int ai_protocol;
87 socklen_t ai_addrlen;
88 struct sockaddr *ai_addr;
89 char *ai_canonname;
90 struct addrinfo *ai_next;
91 };
92 .EE
93 .in
94 .P
95 The
96 .I hints
97 argument points to an
98 .I addrinfo
99 structure that specifies criteria for selecting the socket address
100 structures returned in the list pointed to by
101 .IR res .
102 If
103 .I hints
104 is not NULL it points to an
105 .I addrinfo
106 structure whose
107 .IR ai_family ,
108 .IR ai_socktype ,
109 and
110 .I ai_protocol
111 specify criteria that limit the set of socket addresses returned by
112 .BR getaddrinfo (),
113 as follows:
114 .TP
115 .I ai_family
116 This field specifies the desired address family for the returned addresses.
117 Valid values for this field include
118 .B AF_INET
119 and
120 .BR AF_INET6 .
121 The value
122 .B AF_UNSPEC
123 indicates that
124 .BR getaddrinfo ()
125 should return socket addresses for any address family
126 (either IPv4 or IPv6, for example) that can be used with
127 .I node
128 and
129 .IR service .
130 .TP
131 .I ai_socktype
132 This field specifies the preferred socket type, for example
133 .B SOCK_STREAM
134 or
135 .BR SOCK_DGRAM .
136 Specifying 0 in this field indicates that socket addresses of any type
137 can be returned by
138 .BR getaddrinfo ().
139 .TP
140 .I ai_protocol
141 This field specifies the protocol for the returned socket addresses.
142 Specifying 0 in this field indicates that socket addresses with
143 any protocol can be returned by
144 .BR getaddrinfo ().
145 .TP
146 .I ai_flags
147 This field specifies additional options, described below.
148 Multiple flags are specified by bitwise OR-ing them together.
149 .P
150 All the other fields in the structure pointed to by
151 .I hints
152 must contain either 0 or a null pointer, as appropriate.
153 .P
154 Specifying
155 .I hints
156 as NULL is equivalent to setting
157 .I ai_socktype
158 and
159 .I ai_protocol
160 to 0;
161 .I ai_family
162 to
163 .BR AF_UNSPEC ;
164 and
165 .I ai_flags
166 to
167 .BR "(AI_V4MAPPED\ |\ AI_ADDRCONFIG)" .
168 (POSIX specifies different defaults for
169 .IR ai_flags ;
170 see NOTES.)
171 .I node
172 specifies either a numerical network address
173 (for IPv4, numbers-and-dots notation as supported by
174 .BR inet_aton (3);
175 for IPv6, hexadecimal string format as supported by
176 .BR inet_pton (3)),
177 or a network hostname, whose network addresses are looked up and resolved.
178 If
179 .I hints.ai_flags
180 contains the
181 .B AI_NUMERICHOST
182 flag, then
183 .I node
184 must be a numerical network address.
185 The
186 .B AI_NUMERICHOST
187 flag suppresses any potentially lengthy network host address lookups.
188 .P
189 If the
190 .B AI_PASSIVE
191 flag is specified in
192 .IR hints.ai_flags ,
193 and
194 .I node
195 is NULL,
196 then the returned socket addresses will be suitable for
197 .BR bind (2)ing
198 a socket that will
199 .BR accept (2)
200 connections.
201 The returned socket address will contain the "wildcard address"
202 .RB ( INADDR_ANY
203 for IPv4 addresses,
204 .B IN6ADDR_ANY_INIT
205 for IPv6 address).
206 The wildcard address is used by applications (typically servers)
207 that intend to accept connections on any of the host's network addresses.
208 If
209 .I node
210 is not NULL, then the
211 .B AI_PASSIVE
212 flag is ignored.
213 .P
214 If the
215 .B AI_PASSIVE
216 flag is not set in
217 .IR hints.ai_flags ,
218 then the returned socket addresses will be suitable for use with
219 .BR connect (2),
220 .BR sendto (2),
221 or
222 .BR sendmsg (2).
223 If
224 .I node
225 is NULL,
226 then the network address will be set to the loopback interface address
227 .RB ( INADDR_LOOPBACK
228 for IPv4 addresses,
229 .B IN6ADDR_LOOPBACK_INIT
230 for IPv6 address);
231 this is used by applications that intend to communicate
232 with peers running on the same host.
233 .P
234 .I service
235 sets the port in each returned address structure.
236 If this argument is a service name (see
237 .BR services (5)),
238 it is translated to the corresponding port number.
239 This argument can also be specified as a decimal number,
240 which is simply converted to binary.
241 If
242 .I service
243 is NULL, then the port number of the returned socket addresses
244 will be left uninitialized.
245 If
246 .B AI_NUMERICSERV
247 is specified in
248 .I hints.ai_flags
249 and
250 .I service
251 is not NULL, then
252 .I service
253 must point to a string containing a numeric port number.
254 This flag is used to inhibit the invocation of a name resolution service
255 in cases where it is known not to be required.
256 .P
257 Either
258 .I node
259 or
260 .IR service ,
261 but not both, may be NULL.
262 .P
263 The
264 .BR getaddrinfo ()
265 function allocates and initializes a linked list of
266 .I addrinfo
267 structures, one for each network address that matches
268 .I node
269 and
270 .IR service ,
271 subject to any restrictions imposed by
272 .IR hints ,
273 and returns a pointer to the start of the list in
274 .IR res .
275 The items in the linked list are linked by the
276 .I ai_next
277 field.
278 .P
279 There are several reasons why
280 the linked list may have more than one
281 .I addrinfo
282 structure, including: the network host is multihomed, accessible
283 over multiple protocols (e.g., both
284 .B AF_INET
285 and
286 .BR AF_INET6 );
287 or the same service is available from multiple socket types (one
288 .B SOCK_STREAM
289 address and another
290 .B SOCK_DGRAM
291 address, for example).
292 Normally, the application should try
293 using the addresses in the order in which they are returned.
294 The sorting function used within
295 .BR getaddrinfo ()
296 is defined in RFC\ 3484;
297 the order can be tweaked for a particular system
298 by editing
299 .I /etc/gai.conf
300 (available since glibc 2.5).
301 .P
302 If
303 .I hints.ai_flags
304 includes the
305 .B AI_CANONNAME
306 flag, then the
307 .I ai_canonname
308 field of the first of the
309 .I addrinfo
310 structures in the returned list is set to point to the
311 official name of the host.
312 .\" Prior to glibc 2.3.4, the ai_canonname of each addrinfo
313 .\" structure was set pointing to the canonical name; that was
314 .\" more than POSIX.1-2001 specified, or other implementations provided.
315 .\" MTK, Aug 05
316 .P
317 The remaining fields of each returned
318 .I addrinfo
319 structure are initialized as follows:
320 .IP \[bu] 3
321 The
322 .IR ai_family ,
323 .IR ai_socktype ,
324 and
325 .I ai_protocol
326 fields return the socket creation parameters (i.e., these fields have
327 the same meaning as the corresponding arguments of
328 .BR socket (2)).
329 For example,
330 .I ai_family
331 might return
332 .B AF_INET
333 or
334 .BR AF_INET6 ;
335 .I ai_socktype
336 might return
337 .B SOCK_DGRAM
338 or
339 .BR SOCK_STREAM ;
340 and
341 .I ai_protocol
342 returns the protocol for the socket.
343 .IP \[bu]
344 A pointer to the socket address is placed in the
345 .I ai_addr
346 field, and the size of the socket address, in bytes,
347 is placed in the
348 .I ai_addrlen
349 field.
350 .P
351 If
352 .I hints.ai_flags
353 includes the
354 .B AI_ADDRCONFIG
355 flag, then IPv4 addresses are returned in the list pointed to by
356 .I res
357 only if the local system has at least one
358 IPv4 address configured, and IPv6 addresses are returned
359 only if the local system has at least one IPv6 address configured.
360 The loopback address is not considered for this case as valid
361 as a configured address.
362 This flag is useful on, for example,
363 IPv4-only systems, to ensure that
364 .BR getaddrinfo ()
365 does not return IPv6 socket addresses that would always fail in
366 .BR connect (2)
367 or
368 .BR bind (2).
369 .P
370 If
371 .I hints.ai_flags
372 specifies the
373 .B AI_V4MAPPED
374 flag, and
375 .I hints.ai_family
376 was specified as
377 .BR AF_INET6 ,
378 and no matching IPv6 addresses could be found,
379 then return IPv4-mapped IPv6 addresses in the list pointed to by
380 .IR res .
381 If both
382 .B AI_V4MAPPED
383 and
384 .B AI_ALL
385 are specified in
386 .IR hints.ai_flags ,
387 then return both IPv6 and IPv4-mapped IPv6 addresses
388 in the list pointed to by
389 .IR res .
390 .B AI_ALL
391 is ignored if
392 .B AI_V4MAPPED
393 is not also specified.
394 .P
395 The
396 .BR freeaddrinfo ()
397 function frees the memory that was allocated
398 for the dynamically allocated linked list
399 .IR res .
400 .SS Extensions to getaddrinfo() for Internationalized Domain Names
401 Starting with glibc 2.3.4,
402 .BR getaddrinfo ()
403 has been extended to selectively allow the incoming and outgoing
404 hostnames to be transparently converted to and from the
405 Internationalized Domain Name (IDN) format (see RFC 3490,
406 .IR "Internationalizing Domain Names in Applications (IDNA)" ).
407 Four new flags are defined:
408 .TP
409 .B AI_IDN
410 If this flag is specified, then the node name given in
411 .I node
412 is converted to IDN format if necessary.
413 The source encoding is that of the current locale.
414 .IP
415 If the input name contains non-ASCII characters, then the IDN encoding
416 is used.
417 Those parts of the node name (delimited by dots) that contain
418 non-ASCII characters are encoded using ASCII Compatible Encoding (ACE)
419 before being passed to the name resolution functions.
420 .\" Implementation Detail:
421 .\" To minimize effects on system performance the implementation might
422 .\" want to check whether the input string contains any non-ASCII
423 .\" characters. If there are none the IDN step can be skipped completely.
424 .\" On systems which allow not-ASCII safe encodings for a locale this
425 .\" might be a problem.
426 .TP
427 .B AI_CANONIDN
428 After a successful name lookup, and if the
429 .B AI_CANONNAME
430 flag was specified,
431 .BR getaddrinfo ()
432 will return the canonical name of the
433 node corresponding to the
434 .I addrinfo
435 structure value passed back.
436 The return value is an exact copy of the value returned by the name
437 resolution function.
438 .IP
439 If the name is encoded using ACE, then it will contain the
440 .I xn\-\-
441 prefix for one or more components of the name.
442 To convert these components into a readable form the
443 .B AI_CANONIDN
444 flag can be passed in addition to
445 .BR AI_CANONNAME .
446 The resulting string is encoded using the current locale's encoding.
447 .\"
448 .\"Implementation Detail:
449 .\"If no component of the returned name starts with xn\-\- the IDN
450 .\"step can be skipped, therefore avoiding unnecessary slowdowns.
451 .TP
452 .B AI_IDN_ALLOW_UNASSIGNED
453 .TQ
454 .B AI_IDN_USE_STD3_ASCII_RULES
455 Setting these flags will enable the
456 IDNA_ALLOW_UNASSIGNED (allow unassigned Unicode code points) and
457 IDNA_USE_STD3_ASCII_RULES (check output to make sure it is a STD3
458 conforming hostname)
459 flags respectively to be used in the IDNA handling.
460 .SH RETURN VALUE
461 .\" FIXME glibc defines the following additional errors, some which
462 .\" can probably be returned by getaddrinfo(); they need to
463 .\" be documented.
464 .\" #ifdef __USE_GNU
465 .\" #define EAI_INPROGRESS -100 /* Processing request in progress. */
466 .\" #define EAI_CANCELED -101 /* Request canceled. */
467 .\" #define EAI_NOTCANCELED -102 /* Request not canceled. */
468 .\" #define EAI_ALLDONE -103 /* All requests done. */
469 .\" #define EAI_INTR -104 /* Interrupted by a signal. */
470 .\" #define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
471 .\" #endif
472 .BR getaddrinfo ()
473 returns 0 if it succeeds, or one of the following nonzero error codes:
474 .TP
475 .B EAI_ADDRFAMILY
476 .\" Not in SUSv3
477 The specified network host does not have any network addresses in the
478 requested address family.
479 .TP
480 .B EAI_AGAIN
481 The name server returned a temporary failure indication.
482 Try again later.
483 .TP
484 .B EAI_BADFLAGS
485 .I hints.ai_flags
486 contains invalid flags;
487 or,
488 .I hints.ai_flags
489 included
490 .B AI_CANONNAME
491 and
492 .I node
493 was NULL.
494 .TP
495 .B EAI_FAIL
496 The name server returned a permanent failure indication.
497 .TP
498 .B EAI_FAMILY
499 The requested address family is not supported.
500 .TP
501 .B EAI_MEMORY
502 Out of memory.
503 .TP
504 .B EAI_NODATA
505 .\" Not in SUSv3
506 The specified network host exists, but does not have any
507 network addresses defined.
508 .TP
509 .B EAI_NONAME
510 The
511 .I node
512 or
513 .I service
514 is not known;
515 or both
516 .I node
517 and
518 .I service
519 are NULL;
520 or
521 .B AI_NUMERICSERV
522 was specified in
523 .I hints.ai_flags
524 and
525 .I service
526 was not a numeric port-number string.
527 .TP
528 .B EAI_SERVICE
529 The requested service is not available for the requested socket type.
530 It may be available through another socket type.
531 For example, this error could occur if
532 .I service
533 was "shell" (a service available only on stream sockets), and either
534 .I hints.ai_protocol
535 was
536 .BR IPPROTO_UDP ,
537 or
538 .I hints.ai_socktype
539 was
540 .BR SOCK_DGRAM ;
541 or the error could occur if
542 .I service
543 was not NULL, and
544 .I hints.ai_socktype
545 was
546 .B SOCK_RAW
547 (a socket type that does not support the concept of services).
548 .TP
549 .B EAI_SOCKTYPE
550 The requested socket type is not supported.
551 This could occur, for example, if
552 .I hints.ai_socktype
553 and
554 .I hints.ai_protocol
555 are inconsistent (e.g.,
556 .B SOCK_DGRAM
557 and
558 .BR IPPROTO_TCP ,
559 respectively).
560 .TP
561 .B EAI_SYSTEM
562 Other system error;
563 .I errno
564 is set to indicate the error.
565 .P
566 The
567 .BR gai_strerror ()
568 function translates these error codes to a human readable string,
569 suitable for error reporting.
570 .SH FILES
571 .I /etc/gai.conf
572 .SH ATTRIBUTES
573 For an explanation of the terms used in this section, see
574 .BR attributes (7).
575 .TS
576 allbox;
577 lbx lb lb
578 l l l.
579 Interface Attribute Value
580 T{
581 .na
582 .nh
583 .BR getaddrinfo ()
584 T} Thread safety MT-Safe env locale
585 T{
586 .na
587 .nh
588 .BR freeaddrinfo (),
589 .BR gai_strerror ()
590 T} Thread safety MT-Safe
591 .TE
592 .SH VERSIONS
593 According to POSIX.1, specifying
594 .\" POSIX.1-2001, POSIX.1-2008
595 .I hints
596 as NULL should cause
597 .I ai_flags
598 to be assumed as 0.
599 The GNU C library instead assumes a value of
600 .B (AI_V4MAPPED\~|\~AI_ADDRCONFIG)
601 for this case,
602 since this value is considered an improvement on the specification.
603 .SH STANDARDS
604 POSIX.1-2008.
605 .TP
606 .BR getaddrinfo ()
607 RFC\ 2553.
608 .SH HISTORY
609 POSIX.1-2001.
610 .TP
611 .B AI_ADDRCONFIG
612 .TQ
613 .B AI_ALL
614 .TQ
615 .B AI_V4MAPPED
616 glibc 2.3.3.
617 .TP
618 .B AI_NUMERICSERV
619 glibc 2.3.4.
620 .SH NOTES
621 .BR getaddrinfo ()
622 supports the
623 .IB address % scope-id
624 notation for specifying the IPv6 scope-ID.
625 .SH EXAMPLES
626 .\" getnameinfo.3 refers to this example
627 .\" socket.2 refers to this example
628 .\" bind.2 refers to this example
629 .\" connect.2 refers to this example
630 .\" recvfrom.2 refers to this example
631 .\" sendto.2 refers to this example
632 The following programs demonstrate the use of
633 .BR getaddrinfo (),
634 .BR gai_strerror (),
635 .BR freeaddrinfo (),
636 and
637 .BR getnameinfo (3).
638 The programs are an echo server and client for UDP datagrams.
639 .SS Server program
640 \&
641 .\" SRC BEGIN (server.c)
642 .EX
643 #include <netdb.h>
644 #include <stdio.h>
645 #include <stdlib.h>
646 #include <string.h>
647 #include <sys/socket.h>
648 #include <sys/types.h>
649 #include <unistd.h>
650 \&
651 #define BUF_SIZE 500
652 \&
653 int
654 main(int argc, char *argv[])
655 {
656 int sfd, s;
657 char buf[BUF_SIZE];
658 ssize_t nread;
659 socklen_t peer_addrlen;
660 struct addrinfo hints;
661 struct addrinfo *result, *rp;
662 struct sockaddr_storage peer_addr;
663 \&
664 if (argc != 2) {
665 fprintf(stderr, "Usage: %s port\[rs]n", argv[0]);
666 exit(EXIT_FAILURE);
667 }
668 \&
669 memset(&hints, 0, sizeof(hints));
670 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
671 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
672 hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
673 hints.ai_protocol = 0; /* Any protocol */
674 hints.ai_canonname = NULL;
675 hints.ai_addr = NULL;
676 hints.ai_next = NULL;
677 \&
678 s = getaddrinfo(NULL, argv[1], &hints, &result);
679 if (s != 0) {
680 fprintf(stderr, "getaddrinfo: %s\[rs]n", gai_strerror(s));
681 exit(EXIT_FAILURE);
682 }
683 \&
684 /* getaddrinfo() returns a list of address structures.
685 Try each address until we successfully bind(2).
686 If socket(2) (or bind(2)) fails, we (close the socket
687 and) try the next address. */
688 \&
689 for (rp = result; rp != NULL; rp = rp\->ai_next) {
690 sfd = socket(rp\->ai_family, rp\->ai_socktype,
691 rp\->ai_protocol);
692 if (sfd == \-1)
693 continue;
694 \&
695 if (bind(sfd, rp\->ai_addr, rp\->ai_addrlen) == 0)
696 break; /* Success */
697 \&
698 close(sfd);
699 }
700 \&
701 freeaddrinfo(result); /* No longer needed */
702 \&
703 if (rp == NULL) { /* No address succeeded */
704 fprintf(stderr, "Could not bind\[rs]n");
705 exit(EXIT_FAILURE);
706 }
707 \&
708 /* Read datagrams and echo them back to sender. */
709 \&
710 for (;;) {
711 char host[NI_MAXHOST], service[NI_MAXSERV];
712 \&
713 peer_addrlen = sizeof(peer_addr);
714 nread = recvfrom(sfd, buf, BUF_SIZE, 0,
715 (struct sockaddr *) &peer_addr, &peer_addrlen);
716 if (nread == \-1)
717 continue; /* Ignore failed request */
718 \&
719 s = getnameinfo((struct sockaddr *) &peer_addr,
720 peer_addrlen, host, NI_MAXHOST,
721 service, NI_MAXSERV, NI_NUMERICSERV);
722 if (s == 0)
723 printf("Received %zd bytes from %s:%s\[rs]n",
724 nread, host, service);
725 else
726 fprintf(stderr, "getnameinfo: %s\[rs]n", gai_strerror(s));
727 \&
728 if (sendto(sfd, buf, nread, 0, (struct sockaddr *) &peer_addr,
729 peer_addrlen) != nread)
730 {
731 fprintf(stderr, "Error sending response\[rs]n");
732 }
733 }
734 }
735 .EE
736 .\" SRC END
737 .SS Client program
738 \&
739 .\" SRC BEGIN (client.c)
740 .EX
741 #include <netdb.h>
742 #include <stdio.h>
743 #include <stdlib.h>
744 #include <string.h>
745 #include <sys/socket.h>
746 #include <sys/types.h>
747 #include <unistd.h>
748 \&
749 #define BUF_SIZE 500
750 \&
751 int
752 main(int argc, char *argv[])
753 {
754 int sfd, s;
755 char buf[BUF_SIZE];
756 size_t size;
757 ssize_t nread;
758 struct addrinfo hints;
759 struct addrinfo *result, *rp;
760 \&
761 if (argc < 3) {
762 fprintf(stderr, "Usage: %s host port msg...\[rs]n", argv[0]);
763 exit(EXIT_FAILURE);
764 }
765 \&
766 /* Obtain address(es) matching host/port. */
767 \&
768 memset(&hints, 0, sizeof(hints));
769 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
770 hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
771 hints.ai_flags = 0;
772 hints.ai_protocol = 0; /* Any protocol */
773 \&
774 s = getaddrinfo(argv[1], argv[2], &hints, &result);
775 if (s != 0) {
776 fprintf(stderr, "getaddrinfo: %s\[rs]n", gai_strerror(s));
777 exit(EXIT_FAILURE);
778 }
779 \&
780 /* getaddrinfo() returns a list of address structures.
781 Try each address until we successfully connect(2).
782 If socket(2) (or connect(2)) fails, we (close the socket
783 and) try the next address. */
784 \&
785 for (rp = result; rp != NULL; rp = rp\->ai_next) {
786 sfd = socket(rp\->ai_family, rp\->ai_socktype,
787 rp\->ai_protocol);
788 if (sfd == \-1)
789 continue;
790 \&
791 if (connect(sfd, rp\->ai_addr, rp\->ai_addrlen) != \-1)
792 break; /* Success */
793 \&
794 close(sfd);
795 }
796 \&
797 freeaddrinfo(result); /* No longer needed */
798 \&
799 if (rp == NULL) { /* No address succeeded */
800 fprintf(stderr, "Could not connect\[rs]n");
801 exit(EXIT_FAILURE);
802 }
803 \&
804 /* Send remaining command\-line arguments as separate
805 datagrams, and read responses from server. */
806 \&
807 for (size_t j = 3; j < argc; j++) {
808 size = strlen(argv[j]) + 1;
809 /* +1 for terminating null byte */
810 \&
811 if (size > BUF_SIZE) {
812 fprintf(stderr,
813 "Ignoring long message in argument %zu\[rs]n", j);
814 continue;
815 }
816 \&
817 if (write(sfd, argv[j], size) != size) {
818 fprintf(stderr, "partial/failed write\[rs]n");
819 exit(EXIT_FAILURE);
820 }
821 \&
822 nread = read(sfd, buf, BUF_SIZE);
823 if (nread == \-1) {
824 perror("read");
825 exit(EXIT_FAILURE);
826 }
827 \&
828 printf("Received %zd bytes: %s\[rs]n", nread, buf);
829 }
830 \&
831 exit(EXIT_SUCCESS);
832 }
833 .EE
834 .\" SRC END
835 .SH SEE ALSO
836 .\" .BR getipnodebyaddr (3),
837 .\" .BR getipnodebyname (3),
838 .BR getaddrinfo_a (3),
839 .BR gethostbyname (3),
840 .BR getnameinfo (3),
841 .BR inet (3),
842 .BR gai.conf (5),
843 .BR hostname (7),
844 .BR ip (7)