Interview Questions

Code Sample:Network Address Conversion Routines

Beginners Guide to Sockets


(Continued from previous question...)

Code Sample:Network Address Conversion Routines

Network Address Conversion Routines


#include &ltsys/types.h>
#include &ltsys/socket.h>
#include &ltnetinet/in.h> 
    /* in_addr structure */

unsigned long inet_addr(char *ptr)

char *inet_ntoa(struct in_addr inaddr)

inet_addr() converts a character string in dotted-decimal notation to a 32-bit Internet address. The return value is not consistent, unfortunately. The correct return should be a pointer to a structure of type in_addr but many systems, following an older convention, return only the internal representation of the dotted-decimal notation. The man pages will clarify the situation for the host system on which the function is used.

inet_ntoa() expects a structure of type in_addr as a parameter (note that the structure itself is passed, not a pointer) and returns a pointer to a character string containing the dotted-decimal representation of the Internet address.

(Continued on next question...)

Other Interview Questions