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

📄 socket.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
This specifies all four bytes of the address individually.@item @var{a}.@var{b}.@var{c}The last part of the address, @var{c}, 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{b}}.@item @var{a}.@var{b}The last part of the address, @var{c}, is interpreted as a 3-byte quantity.This is useful for specifying host addresses in a Class A network withnetwork address number @var{a}.@item @var{a}If only one part is given, this corresponds directly to the host addressnumber.@end tableWithin each part of the address, the usual C conventions for specifyingthe radix apply.  In other words, a leading @samp{0x} or @samp{0X} implieshexadecimal radix; a leading @samp{0} implies octal; and otherwise decimalradix is assumed.@node Host Address Data Type@subsubsection Host Address Data TypeInternet host addresses are represented in some contexts as integers(type @code{unsigned long int}).  In other contexts, the integer ispackaged inside a structure of type @code{struct in_addr}.  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.The following basic definitions for Internet addresses appear in theheader file @file{netinet/in.h}:@pindex netinet/in.h@comment netinet/in.h@comment BSD@deftp {Data Type} {struct in_addr}This data type is used in certain contexts to contain an Internet hostaddress.  It has just one field, named @code{s_addr}, which records thehost address number as an @code{unsigned long int}.@end deftp@comment netinet/in.h@comment BSD@deftypevr Macro {unsigned long int} INADDR_LOOPBACKYou can use this constant to stand for ``the address of this machine,''instead of finding its actual address.  It is the Internet address@samp{127.0.0.1}, which is usually called @samp{localhost}.  Thisspecial constant saves you the trouble of looking up the address of yourown machine.  Also, the system usually implements @code{INADDR_LOOPBACK}specially, avoiding any network traffic for the case of one machinetalking to itself.@end deftypevr@comment netinet/in.h@comment BSD@deftypevr Macro {unsigned long int} INADDR_ANYYou can use this constant to stand for ``any incoming address,'' whenbinding to an address.  @xref{Setting Address}.  This is the usualaddress to give in the @code{sin_addr} member of @w{@code{structsockaddr_in}} when you want to accept Internet connections.@end deftypevr@comment netinet/in.h@comment BSD@deftypevr Macro {unsigned long int} INADDR_BROADCASTThis constant is the address you use to send a broadcast message.@c !!! broadcast needs further documented@end deftypevr@comment netinet/in.h@comment BSD@deftypevr Macro {unsigned long int} INADDR_NONEThis constant is returned by some functions to indicate an error.@end deftypevr@node Host Address Functions@subsubsection Host Address Functions@pindex arpa/inet.hThese additional functions for manipulating Internet addresses aredeclared in @file{arpa/inet.h}.  They represent Internet addresses innetwork byte order; they represent network numbers andlocal-address-within-network numbers in host byte order.@xref{Byte Order}, for an explanation of network and host byte order.@comment arpa/inet.h@comment BSD@deftypefun {int} inet_aton (const char *@var{name}, struct in_addr *@var{addr})This function converts the Internet host address @var{name}from the standard numbers-and-dots notation into binary data and storesit in the @code{struct in_addr} that @var{addr} points to.@code{inet_aton} returns nonzero if the address is valid, zero if not.@end deftypefun@comment arpa/inet.h@comment BSD@deftypefun {unsigned long int} inet_addr (const char *@var{name})This function converts the Internet host address @var{name} from thestandard numbers-and-dots notation into binary data.  If the input isnot valid, @code{inet_addr} returns @code{INADDR_NONE}.  This is anobsolete interface to @code{inet_aton}, described immediately above; itis obsolete because @code{INADDR_NONE} is a valid address(255.255.255.255), and @code{inet_aton} provides a cleaner way toindicate error return.@end deftypefun@comment arpa/inet.h@comment BSD@deftypefun {unsigned long int} inet_network (const char *@var{name})This function extracts the network number from the address @var{name},given in the standard numbers-and-dots notation.If the input is not valid, @code{inet_network} returns @code{-1}.@end deftypefun@comment arpa/inet.h@comment BSD@deftypefun {char *} inet_ntoa (struct in_addr @var{addr})This function converts the Internet host address @var{addr} 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.@end deftypefun@comment arpa/inet.h@comment BSD@deftypefun {struct in_addr} inet_makeaddr (int @var{net}, int @var{local})This function makes an Internet host address by combining the networknumber @var{net} with the local-address-within-network number@var{local}.@end deftypefun@comment arpa/inet.h@comment BSD@deftypefun int inet_lnaof (struct in_addr @var{addr})This function returns the local-address-within-network part of theInternet host address @var{addr}.@end deftypefun@comment arpa/inet.h@comment BSD@deftypefun int inet_netof (struct in_addr @var{addr})This function returns the network number part of the Internet hostaddress @var{addr}.@end deftypefun@node Host Names@subsubsection Host Names@cindex hosts database@cindex converting host name to address@cindex converting host address to nameBesides 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} is also known as@samp{churchy.gnu.ai.mit.edu}; and other machines in the @samp{gnu.ai.mit.edu}domain can refer to it simply as @samp{churchy}.@pindex /etc/hosts@pindex netdb.hInternally, the system uses a database to keep track of the mappingbetween host names and host numbers.  This database is usually eitherthe file @file{/etc/hosts} or an equivalent provided by a name server.The functions and other symbols for accessing this database are declaredin @file{netdb.h}.  They are BSD features, defined unconditionally ifyou include @file{netdb.h}.@comment netdb.h@comment BSD@deftp {Data Type} {struct hostent}This data type is used to represent an entry in the hosts database.  Ithas the following members:@table @code@item char *h_nameThis is the ``official'' name of the host.@item char **h_aliasesThese are alternative names for the host, represented as a null-terminatedvector of strings.@item int h_addrtypeThis is the host address type; in practice, its value is always@code{AF_INET}.  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}.@xref{Socket Addresses}.@item int h_lengthThis is the length, in bytes, of each address.@item char **h_addr_listThis 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.@item char *h_addrThis is a synonym for @code{h_addr_list[0]}; in other words, it is thefirst host address.@end table@end deftpAs far as the host database is concerned, each address is just a blockof memory @code{h_length} bytes long.  But in other contexts there is animplicit assumption that you can convert this to a @code{struct in_addr} oran @code{unsigned long int}.  Host addresses in a @code{struct hostent}structure are always given in network byte order; see @ref{Byte Order}.You can use @code{gethostbyname} or @code{gethostbyaddr} 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.@comment netdb.h@comment BSD@deftypefun {struct hostent *} gethostbyname (const char *@var{name})The @code{gethostbyname} function returns information about the hostnamed @var{name}.  If the lookup fails, it returns a null pointer.@end deftypefun@comment netdb.h@comment BSD@deftypefun {struct hostent *} gethostbyaddr (const char *@var{addr}, int @var{length}, int @var{format})The @code{gethostbyaddr} function returns information about the hostwith Internet address @var{addr}.  The @var{length} argument is thesize (in bytes) of the address at @var{addr}.  @var{format} specifiesthe address format; for an Internet address, specify a value of@code{AF_INET}.If the lookup fails, @code{gethostbyaddr} returns a null pointer.@end deftypefun@vindex h_errnoIf the name lookup by @code{gethostbyname} or @code{gethostbyaddr}fails, you can find out the reason by looking at the value of thevariable @code{h_errno}.  (It would be cleaner design for thesefunctions to set @code{errno}, but use of @code{h_errno} is compatiblewith other systems.)  Before using @code{h_errno}, you must declare itlike this:@smallexampleextern int h_errno;@end smallexampleHere are the error codes that you may find in @code{h_errno}:@table @code@comment netdb.h@comment BSD@item HOST_NOT_FOUND@vindex HOST_NOT_FOUNDNo such host is known in the data base.@comment netdb.h@comment BSD@item TRY_AGAIN@vindex TRY_AGAINThis condition happens when the name server could not be contacted.  Ifyou try again later, you may succeed then.@comment netdb.h@comment BSD @item NO_RECOVERY @vindex NO_RECOVERY A non-recoverable error occurred.@comment netdb.h@comment BSD@item NO_ADDRESS@vindex NO_ADDRESSThe host database contains an entry for the name, but it doesn't have anassociated Internet address.@end tableYou can also scan the entire hosts database one entry at a time using@code{sethostent}, @code{gethostent}, and @code{endhostent}.  Be carefulin using these functions, because they are not reentrant.@comment netdb.h@comment BSD@deftypefun void sethostent (int @var{stayopen})This function opens the hosts database to begin scanning it.  You canthen call @code{gethostent} to read the entries.@c There was a rumor that this flag has different meaning if using the DNS,@c but it appears this description is accurate in that case also.If the @var{stayopen} argument is nonzero, this sets a flag so thatsubsequent calls to @code{gethostbyname} or @code{gethostbyaddr} willnot close the database (as they usually would).  This makes for moreefficiency if you call those functions several times, by avoidingreopening the database for each call.@end deftypefun@comment netdb.h@comment BSD@deftypefun {struct hostent *} gethostent ()This function returns the next entry in the hosts database.  Itreturns a null pointer if there are no more entries.@end deftypefun@comment netdb.h@comment BSD@deftypefun void endhostent ()This function closes the hosts database.@end deftypefun@node Ports@subsection Internet Ports@cindex port numberA socket address in the Internet namespace consists of a machine'sInternet address plus a @dfn{port number} which distinguishes thesockets on a given machine (for a given protocol).  Port numbers rangefrom 0 to 65,535.Port numbers less than @code{IPPORT_RESERVED} are reserved for standardservers, such as @code{finger} and @code{telnet}.  There is a databasethat keeps track of these, and you can use the @code{getservbyname}function to map a service name onto a port number; see @ref{ServicesDatabase}.If you write a server that is not one of the standard ones defined inthe database, you must choose a port number for it.  Use a numbergreater than @code{IPPORT_USERRESERVED}; such numbers are reserved forservers and won't ever be generated automatically by the system.Avoiding conflicts with servers being run by other users is up to you.When you use a socket without specifying its address, the systemgenerates a port number for it.  This number is between@code{IPPORT_RESERVED} and @code{IPPORT_USERRESERVED}.On the Internet, it is actually legitimate to have two differentsockets with the same port number, as long as they never both try tocommunicate with the same socket address (host address plus portnumber).  You shouldn't duplicate a port number except in specialcircumstances where a higher-level protocol requires it.  Normally,the system won't let you do it; @code{bind} normally insists ondistinct port numbers.  To reuse a port number, you must set thesocket option @code{SO_REUSEADDR}.  @xref{Socket-Level Options}.@pindex netinet/in.hThese macros are defined in the header file @file{netinet/in.h}.@comment netinet/in.h@comment BSD@deftypevr Macro int IPPORT_RESERVEDPort numbers less than @code{IPPORT_RESERVED} are reserved forsuperuser use.@end deftypevr

⌨️ 快捷键说明

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