⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library_15.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<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 storehere.<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 ifyou 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 fournumbers, 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 stringsof 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 numericaddresses and host names.  But the program needs a numeric address toopen a connection; to use a host name, you must convert it to thenumeric 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 networknumber consists of the first one, two or three bytes; the rest of thebytes 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 localnetwork address numbers of individual machines are registered with theadministrator of the particular network.<P>Class A networks have single-byte numbers in the range 0 to 127.  Thereare only a small number of Class A networks, but they can each support avery large number of hosts.  Medium-sized Class B networks have two-bytenetwork numbers, with the first byte in the range 128 to 191.  Class Cnetworks are the smallest; they have three-byte network numbers, withthe first byte in the range 192-255.  Thus, the first 1, 2, or 3 bytesof an Internet address specifies a network.  The remaining bytes of theInternet address specify the address within that network.<P>The Class A network 0 is reserved for broadcast to all networks.  Inaddition, 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 usethe 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 canhave multiple Internet host addresses.  However, there is neversupposed 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 withnetwork 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 withnetwork 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 addressnumber.</DL><P>Within each part of the address, the usual C conventions for specifyingthe radix apply.  In other words, a leading <SAMP>`0x'</SAMP> or <SAMP>`0X'</SAMP> implieshexadecimal radix; a leading <SAMP>`0'</SAMP> implies octal; and otherwise decimalradix 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 ispackaged inside a structure of type <CODE>struct in_addr</CODE>.  It wouldbe better if the usage were made consistent, but it is not hard to extractthe integer from the structure or put the integer into a structure.<P>The following basic definitions for Internet addresses appear in theheader 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 hostaddress.  It has just one field, named <CODE>s_addr</CODE>, which records thehost 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 youthe trouble of looking up the address of your own machine.  Also, ifyour machine has multiple network addresses on different networks (whichis not unusual), using <CODE>INADDR_ANY</CODE> permits the system to choosewhichever 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 aredeclared in <TT>`arpa/inet.h'</TT>.  They represent Internet addresses innetwork byte order; they represent network numbers andlocal-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 astring in the standard numbers-and-dots notation.  The return value isa pointer into a statically-allocated buffer.  Subsequent calls willoverwrite the same buffer, so you should copy the string if you needto 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 networknumber <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 theInternet 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 hostaddress <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 asymbolic 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 mappingbetween host names and host numbers.  This database is usually eitherthe file <TT>`/etc/hosts'</TT> or an equivalent provided by a name server.The functions and other symbols for accessing this database are declaredin <TT>`netdb.h'</TT>.  They are BSD features, defined unconditionally ifyou 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.  Ithas 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-terminatedvector 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 berepresented in the data base as well as Internet addresses; if this weredone, 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 hostmight be connected to multiple networks and have different addresses oneach 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 thefirst host address.</DL><P>As far as the host database is concerned, each address is just a blockof memory <CODE>h_length</CODE> bytes long.  But in other contexts there is animplicit assumption that you can convert this to a <CODE>struct in_addr</CODE> oran <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 thehosts database for information about a particular host.  The informationis returned in a statically-allocated structure; you must copy theinformation 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 hostnamed <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 hostwith Internet address <VAR>addr</VAR>.  The <VAR>length</VAR> argument is thesize (in bytes) of the address at <VAR>addr</VAR>.  <VAR>format</VAR> specifiesthe 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 thevariable <CODE>h_errno</CODE>.  (It would be cleaner design for thesefunctions to set <CODE>errno</CODE>, but use of <CODE>h_errno</CODE> is compatiblewith other systems.)  Before using <CODE>h_errno</CODE>, you must declare itlike this:<P><PRE>extern int h_errno;</PRE><P>Here are the error codes that you may find in <CODE>h_errno</CODE>:<P><DL COMPACT><A NAME="IDX960"></A><DT><CODE>HOST_NOT_FOUND</CODE><DD>No such host is known in the data base.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -