Using the sethostent(3) Function
The sethostent(3) function permits you, as the application designer, to control how name server queries are performed. This function can improve the overall network performance of your application. The function synopsis is as follows:
void sethostent(int stayopen);
There is one input argument to sethostent(3). The argument stayopen is treated as a Boolean input parameter:
- When TRUE (non-zero), the name server queries are to be performed with a TCP/IP socket, which will remain open with the name server.
- When FALSE (zero), the name server queries will be performed using UDP datagrams as required.
The first case (TRUE) is useful when your application will make frequent name server requests. This is the higher-performance option for many queries. However, if your application only performs one query at startup, then the FALSE setting is more appropriate, because UDP has less network overhead.
Previously, I have showed how the function gethostbyname(3) could be used to perform name server lookups. To cause this program to use a connected TCP socket instead of UDP datagrams, you can add one call to sethostent(3) in the program, Below I have shown a context diff of the differences between lookup.c and lookup2.c. This listing highlights the simple changes that were made. Changes Required to lookup.c to Use a TCP Socket for Name Server Lookups
[sgupta@rhel55x86 chap9]$ diff -c lookup.c lookup2.c
> #define TRUE 1
> #define FALSE 0
In Listing above, the context diff output shows lines added by preceding the line with a + character. From this, you can see that the only instrumental change that was made was that sethostent (TRUE) was called prior to entering the program's for loop. For clarity, macro definitions for TRUE and FALSE were also added, but these were not required.
Post a Comment