• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* this program is used to test that getaddrinfo() works correctly
2  * without a 'hints' argument
3  */
4 
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netdb.h>
8 
9 #include <stdio.h>  /* for printf */
10 #include <string.h> /* for memset */
11 #include <netinet/in.h>  /* for IPPROTO_TCP */
12 
13 #define  SERVER_NAME  "www.android.com"
14 #define  PORT_NUMBER  "9999"
15 
main(void)16 int main(void)
17 {
18     struct addrinfo  hints;
19     struct addrinfo* res;
20     int              ret;
21 
22     /* first, try without any hints */
23     ret = getaddrinfo( SERVER_NAME, PORT_NUMBER, NULL, &res);
24     if (ret != 0) {
25         printf("first getaddrinfo returned error: %s\n", gai_strerror(ret));
26         return 1;
27     }
28 
29     freeaddrinfo(res);
30 
31     /* now try with the hints */
32     memset(&hints, 0, sizeof(hints));
33     hints.ai_family   = AF_UNSPEC;
34     hints.ai_socktype = SOCK_STREAM;
35     hints.ai_protocol = IPPROTO_TCP;
36 
37     ret = getaddrinfo( SERVER_NAME, PORT_NUMBER, &hints, &res );
38     if (ret != 0) {
39         printf("second getaddrinfo returned error: %s\n", gai_strerror(ret));
40         return 1;
41     }
42 
43     return 0;
44 }
45