IP lookup example program

To demonstrate the getaddrinfo() and getnameinfo() functions, we will implement a short program. This program takes a name or IP address for its only argument. It then uses getaddrinfo() to resolve that name or that IP address into an address structure, and the program prints that IP address using getnameinfo() for the text conversion. If multiple addresses are associated with a name, it prints each of them. It also indicates any errors.

To begin with, we need to include our required header for this chapter. We also define AI_ALL for systems that are missing it. The code for this is as follows:

/*lookup.c*/#include "chap05.h"#ifndef AI_ALL#define AI_ALL 0x0100#endi

We can then begin the main() function and check ...

Get Hands-On Network Programming with C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.