📄 library_15.html
字号:
<P>
<A NAME="IDX934"></A>
<U>Data Type:</U> <B>struct sockaddr_in</B><P>
This is the data type used to represent socket addresses in the
Internet namespace. It has the following members:
<P>
<DL COMPACT>
<DT><CODE>short int sin_family</CODE>
<DD>This identifies the address family or format of the socket address.
You should store the value of <CODE>AF_INET</CODE> in this member.
See section <A HREF="library_15.html#SEC219" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC219">Socket Addresses</A>.
<P>
<DT><CODE>struct in_addr sin_addr</CODE>
<DD>This is the Internet address of the host machine. See section <A HREF="library_15.html#SEC229" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC229">Host Addresses</A>, and section <A HREF="library_15.html#SEC233" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC233">Host Names</A>, for how to get a value to store
here.
<P>
<DT><CODE>unsigned short int sin_port</CODE>
<DD>This is the port number. See section <A HREF="library_15.html#SEC234" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC234">Internet Ports</A>.
</DL>
<P>
When you call <CODE>bind</CODE> or <CODE>getsockname</CODE>, you should specify
<CODE>sizeof (struct sockaddr_in)</CODE> as the <VAR>length</VAR> parameter if
you are using an Internet namespace socket address.
<P>
<H3><A NAME="SEC229" HREF="library_toc.html#SEC229" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC229">Host Addresses</A></H3>
<P>
Each computer on the Internet has one or more <DFN>Internet addresses</DFN>,
numbers which identify that computer among all those on the Internet.
Users typically write numeric host addresses as sequences of four
numbers, separated by periods, as in <SAMP>`128.52.46.32'</SAMP>.
<P>
Each computer also has one or more <DFN>host names</DFN>, which are strings
of words separated by periods, as in <SAMP>`churchy.gnu.ai.mit.edu'</SAMP>.
<P>
Programs that let the user specify a host typically accept both numeric
addresses and host names. But the program needs a numeric address to
open a connection; to use a host name, you must convert it to the
numeric address it stands for.
<P>
<A NAME="IDX935"></A>
<A NAME="IDX936"></A>
<H4><A NAME="SEC230" HREF="library_toc.html#SEC230" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC230">Internet Host Addresses</A></H4>
<P>
<A NAME="IDX937"></A>
<A NAME="IDX938"></A>
<P>
An Internet host address is a number containing four bytes of data.
These are divided into two parts, a <DFN>network number</DFN> and a
<DFN>local network address number</DFN> within that network. The network
number consists of the first one, two or three bytes; the rest of the
bytes are the local address.
<P>
Network numbers are registered with the Network Information Center
(NIC), and are divided into three classes--A, B, and C. The local
network address numbers of individual machines are registered with the
administrator of the particular network.
<P>
Class A networks have single-byte numbers in the range 0 to 127. There
are only a small number of Class A networks, but they can each support a
very large number of hosts. Medium-sized Class B networks have two-byte
network numbers, with the first byte in the range 128 to 191. Class C
networks are the smallest; they have three-byte network numbers, with
the first byte in the range 192-255. Thus, the first 1, 2, or 3 bytes
of an Internet address specifies a network. The remaining bytes of the
Internet address specify the address within that network.
<P>
The Class A network 0 is reserved for broadcast to all networks. In
addition, the host number 0 within each network is reserved for broadcast
to all hosts in that network.
<P>
The Class A network 127 is reserved for loopback; you can always use
the Internet address <SAMP>`127.0.0.1'</SAMP> to refer to the host machine.
<P>
Since a single machine can be a member of multiple networks, it can
have multiple Internet host addresses. However, there is never
supposed to be more than one machine with the same host address.
<P>
<A NAME="IDX939"></A>
<A NAME="IDX940"></A>
<P>
There are four forms of the <DFN>standard numbers-and-dots notation</DFN>
for Internet addresses:
<P>
<DL COMPACT>
<DT><CODE><VAR>a</VAR>.<VAR>b</VAR>.<VAR>c</VAR>.<VAR>d</VAR></CODE>
<DD>This specifies all four bytes of the address individually.
<P>
<DT><CODE><VAR>a</VAR>.<VAR>b</VAR>.<VAR>c</VAR></CODE>
<DD>The last part of the address, <VAR>c</VAR>, is interpreted as a 2-byte quantity.
This is useful for specifying host addresses in a Class B network with
network address number <CODE><VAR>a</VAR>.<VAR>b</VAR></CODE>.
<P>
<DT><CODE><VAR>a</VAR>.<VAR>b</VAR></CODE>
<DD>The last part of the address, <VAR>c</VAR>, is interpreted as a 3-byte quantity.
This is useful for specifying host addresses in a Class A network with
network address number <VAR>a</VAR>.
<P>
<DT><CODE><VAR>a</VAR></CODE>
<DD>If only one part is given, this corresponds directly to the host address
number.
</DL>
<P>
Within each part of the address, the usual C conventions for specifying
the radix apply. In other words, a leading <SAMP>`0x'</SAMP> or <SAMP>`0X'</SAMP> implies
hexadecimal radix; a leading <SAMP>`0'</SAMP> implies octal; and otherwise decimal
radix is assumed.
<P>
<H4><A NAME="SEC231" HREF="library_toc.html#SEC231" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC231">Host Address Data Type</A></H4>
<P>
Internet host addresses are represented in some contexts as integers
(type <CODE>unsigned long int</CODE>). In other contexts, the integer is
packaged inside a structure of type <CODE>struct in_addr</CODE>. It would
be better if the usage were made consistent, but it is not hard to extract
the integer from the structure or put the integer into a structure.
<P>
The following basic definitions for Internet addresses appear in the
header file <TT>`netinet/in.h'</TT>:
<A NAME="IDX941"></A>
<P>
<A NAME="IDX942"></A>
<U>Data Type:</U> <B>struct in_addr</B><P>
This data type is used in certain contexts to contain an Internet host
address. It has just one field, named <CODE>s_addr</CODE>, which records the
host address number as an <CODE>unsigned long int</CODE>.
<P>
<A NAME="IDX943"></A>
<U>Macro:</U> unsigned long int <B>INADDR_ANY</B><P>
You can use this constant to stand for "the address of this machine,"
instead of finding its actual address. This special constant saves you
the trouble of looking up the address of your own machine. Also, if
your machine has multiple network addresses on different networks (which
is not unusual), using <CODE>INADDR_ANY</CODE> permits the system to choose
whichever address makes communication most efficient.
<P>
<H4><A NAME="SEC232" HREF="library_toc.html#SEC232" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC232">Host Address Functions</A></H4>
<A NAME="IDX944"></A>
<P>
These additional functions for manipulating Internet addresses are
declared in <TT>`arpa/inet.h'</TT>. They represent Internet addresses in
network byte order; they represent network numbers and
local-address-within-network numbers in host byte order.
See section <A HREF="library_15.html#SEC236" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC236">Byte Order Conversion</A>, for an explanation of network and host byte order.
<P>
<A NAME="IDX945"></A>
<U>Function:</U> unsigned long int <B>inet_addr</B> <I>(const char *<VAR>name</VAR>)</I><P>
This function converts the Internet host address <VAR>name</VAR>
from the standard numbers-and-dots notation into binary data.
If the input is not valid, <CODE>inet_addr</CODE> returns <CODE>-1</CODE>.
<P>
<A NAME="IDX946"></A>
<U>Function:</U> unsigned long int <B>inet_network</B> <I>(const char *<VAR>name</VAR>)</I><P>
This function extracts the network number from the address <VAR>name</VAR>,
given in the standard numbers-and-dots notation.
If the input is not valid, <CODE>inet_network</CODE> returns <CODE>-1</CODE>.
<P>
<A NAME="IDX947"></A>
<U>Function:</U> char * <B>inet_ntoa</B> <I>(struct in_addr <VAR>addr</VAR>)</I><P>
This function converts the Internet host address <VAR>addr</VAR> to a
string in the standard numbers-and-dots notation. The return value is
a pointer into a statically-allocated buffer. Subsequent calls will
overwrite the same buffer, so you should copy the string if you need
to save it.
<P>
<A NAME="IDX948"></A>
<U>Function:</U> struct in_addr <B>inet_makeaddr</B> <I>(int <VAR>net</VAR>, int <VAR>local</VAR>)</I><P>
This function makes an Internet host address by combining the network
number <VAR>net</VAR> with the local-address-within-network number
<VAR>local</VAR>.
<P>
<A NAME="IDX949"></A>
<U>Function:</U> int <B>inet_lnaof</B> <I>(struct in_addr <VAR>addr</VAR>)</I><P>
This function returns the local-address-within-network part of the
Internet host address <VAR>addr</VAR>.
<P>
<A NAME="IDX950"></A>
<U>Function:</U> int <B>inet_netof</B> <I>(struct in_addr <VAR>addr</VAR>)</I><P>
This function returns the network number part of the Internet host
address <VAR>addr</VAR>.
<P>
<A NAME="IDX951"></A>
<A NAME="IDX952"></A>
<A NAME="IDX953"></A>
<H4><A NAME="SEC233" HREF="library_toc.html#SEC233" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC233">Host Names</A></H4>
<P>
Besides the standard numbers-and-dots notation for Internet addresses,
you can also refer to a host by a symbolic name. The advantage of a
symbolic name is that it is usually easier to remember. For example,
the machine with Internet address <SAMP>`128.52.46.32'</SAMP> is also known as
<SAMP>`churchy.gnu.ai.mit.edu'</SAMP>; and other machines in the <SAMP>`gnu.ai.mit.edu'</SAMP>
domain can refer to it simply as <SAMP>`churchy'</SAMP>.
<A NAME="IDX954"></A>
<A NAME="IDX955"></A>
<P>
Internally, the system uses a database to keep track of the mapping
between host names and host numbers. This database is usually either
the file <TT>`/etc/hosts'</TT> or an equivalent provided by a name server.
The functions and other symbols for accessing this database are declared
in <TT>`netdb.h'</TT>. They are BSD features, defined unconditionally if
you include <TT>`netdb.h'</TT>.
<P>
<A NAME="IDX956"></A>
<U>Data Type:</U> <B>struct hostent</B><P>
This data type is used to represent an entry in the hosts database. It
has the following members:
<P>
<DL COMPACT>
<DT><CODE>char *h_name</CODE>
<DD>This is the "official" name of the host.
<P>
<DT><CODE>char **h_aliases</CODE>
<DD>These are alternative names for the host, represented as a null-terminated
vector of strings.
<P>
<DT><CODE>int h_addrtype</CODE>
<DD>This is the host address type; in practice, its value is always
<CODE>AF_INET</CODE>. In principle other kinds of addresses could be
represented in the data base as well as Internet addresses; if this were
done, you might find a value in this field other than <CODE>AF_INET</CODE>.
See section <A HREF="library_15.html#SEC219" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC219">Socket Addresses</A>.
<P>
<DT><CODE>int h_length</CODE>
<DD>This is the length, in bytes, of each address.
<P>
<DT><CODE>char **h_addr_list</CODE>
<DD>This is the vector of addresses for the host. (Recall that the host
might be connected to multiple networks and have different addresses on
each one.) The vector is terminated by a null pointer.
<P>
<DT><CODE>char *h_addr</CODE>
<DD>This is a synonym for <CODE>h_addr_list[0]</CODE>; in other words, it is the
first host address.
</DL>
<P>
As far as the host database is concerned, each address is just a block
of memory <CODE>h_length</CODE> bytes long. But in other contexts there is an
implicit assumption that you can convert this to a <CODE>struct in_addr</CODE> or
an <CODE>unsigned long int</CODE>. Host addresses in a <CODE>struct hostent</CODE>
structure are always given in network byte order; see section <A HREF="library_15.html#SEC236" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC236">Byte Order Conversion</A>.
<P>
You can use <CODE>gethostbyname</CODE> or <CODE>gethostbyaddr</CODE> to search the
hosts database for information about a particular host. The information
is returned in a statically-allocated structure; you must copy the
information if you need to save it across calls.
<P>
<A NAME="IDX957"></A>
<U>Function:</U> struct hostent * <B>gethostbyname</B> <I>(const char *<VAR>name</VAR>)</I><P>
The <CODE>gethostbyname</CODE> function returns information about the host
named <VAR>name</VAR>. If the lookup fails, it returns a null pointer.
<P>
<A NAME="IDX958"></A>
<U>Function:</U> struct hostent * <B>gethostbyaddr</B> <I>(const char *<VAR>addr</VAR>, int <VAR>length</VAR>, int <VAR>format</VAR>)</I><P>
The <CODE>gethostbyaddr</CODE> function returns information about the host
with Internet address <VAR>addr</VAR>. The <VAR>length</VAR> argument is the
size (in bytes) of the address at <VAR>addr</VAR>. <VAR>format</VAR> specifies
the address format; for an Internet address, specify a value of
<CODE>AF_INET</CODE>.
<P>
If the lookup fails, <CODE>gethostbyaddr</CODE> returns a null pointer.
<P>
<A NAME="IDX959"></A>
<P>
If the name lookup by <CODE>gethostbyname</CODE> or <CODE>gethostbyaddr</CODE>
fails, you can find out the reason by looking at the value of the
variable <CODE>h_errno</CODE>. (It would be cleaner design for these
functions to set <CODE>errno</CODE>, but use of <CODE>h_errno</CODE> is compatible
with other systems.) Before using <CODE>h_errno</CODE>, you must declare it
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -