inet_addr()

PROTOTYPE

#include <sockets.h>

in_addr_t inet_addr(const char ptr);

DESCRIPTION

inet_addr() converts an IPv4 address from text using Internet dotted notation to a 32-bit integer in network byte order. ptr points to the string to be converted. Terminated by either a NULL or space character, it should have one of the following formats:

  "a" - where a is a 32-bit number,
  "a.b" - where a is an 8-bit number and b is a 24-bit number,
  "a.b.c" - where a and b are 8-bit numbers and c is a 16-bit number, or
  "a.b.c.d" - where a, b, c, and d are 8-bit numbers.

If there is a leading “0x” or “0X”, the numbers are interpreted as hexadecimal. If there is a leading “0”, they are interpreted as octal. Otherwise, the numbers are interpreted as decimal.

If successful, inet_addr() returns the resulting 32-bit IPv4 address in network byte order. Otherwise, it returns (in_addr_t)-1.

EXAMPLE

  /*-----------------------------------------------------------------*/
  /* Assign time server address to the socket.                       */
  /*-----------------------------------------------------------------*/
  bzero(&addr, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(TIME_PORT);
  addr.sin_addr.s_addr = inet_addr("192.168.1.1");