gtpc1m3z | Transmission Control Protocol/Internet Protocol |
The inet_addr function interprets character strings representing
numbers expressed in standard dotted decimal notation and returns numbers
suitable for use as an internet address.
Format
#include <types.h>
#include <socket.h>
u_long inet_addr(char *cp);
- cp
- A character string in standard dotted decimal notation.
Normal Return
The internet address is returned in network byte order.
Error Return
A return code equal to -1 indicates a character string that is not
valid.
Programming Considerations
- Values specified in standard dotted decimal notation take one of the
following forms:
a.b.c.d
a.b.c
a.b
a
- When you specify a four-part address, each part is interpreted as a byte
of data and assigned, from left to right, to one of the 4 bytes of an internet
address.
- When you specify a three-part address, the last part is interpreted as a
16-bit quantity and placed in the 2 rightmost bytes of the network
address. This makes the three-part address format convenient for
specifying Class B network addresses as 128.net.host.
- When you specify a two-part address, the last part is interpreted as a
24-bit quantity and placed in the 3 rightmost bytes of the network
address. This makes the two-part address format convenient for
specifying Class A network addresses as net.host.
- When you specify a one-part address, the value is stored directly in the
network address space without any rearrangement of its bytes.
- Numbers supplied as address parts in standard dotted decimal notation can
be decimal, hexadecimal, or octal. Numbers are interpreted in C
language syntax. A leading 0x implies hexadecimal; a leading 0
implies octal. A number without a leading 0 implies decimal.
Examples
The following example converts character IP address to network byte
order.
#include <types.h>
#include <socket.h>
·
·
·
struct sockaddr_in server_addr;
·
·
·
servername.sin_addr.s_addr = inet_addr("129.5.24.1");
Related Information
None.