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

📄 socket.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@comment sys/socket.h@comment BSD@deftypefun int bind (int @var{socket}, struct sockaddr *@var{addr}, size_t @var{length})The @code{bind} function assigns an address to the socket@var{socket}.  The @var{addr} and @var{length} arguments specify theaddress; the detailed format of the address depends on the namespace.The first part of the address is always the format designator, whichspecifies a namespace, and says that the address is in the format forthat namespace.The return value is @code{0} on success and @code{-1} on failure.  Thefollowing @code{errno} error conditions are defined for this function:@table @code@item EBADFThe @var{socket} argument is not a valid file descriptor.@item ENOTSOCKThe descriptor @var{socket} is not a socket.@item EADDRNOTAVAILThe specified address is not available on this machine.@item EADDRINUSESome other socket is already using the specified address.@item EINVALThe socket @var{socket} already has an address.@item EACCESYou do not have permission to access the requested address.  (In theInternet domain, only the super-user is allowed to specify a port numberin the range 0 through @code{IPPORT_RESERVED} minus one; see@ref{Ports}.)@end tableAdditional conditions may be possible depending on the particular namespaceof the socket.@end deftypefun@node Reading Address@subsection Reading the Address of a Socket@pindex sys/socket.hUse the function @code{getsockname} to examine the address of anInternet socket.  The prototype for this function is in the header file@file{sys/socket.h}.@comment sys/socket.h@comment BSD@deftypefun int getsockname (int @var{socket}, struct sockaddr *@var{addr}, size_t *@var{length-ptr})The @code{getsockname} function returns information about theaddress of the socket @var{socket} in the locations specified by the@var{addr} and @var{length-ptr} arguments.  Note that the@var{length-ptr} is a pointer; you should initialize it to be theallocation size of @var{addr}, and on return it contains the actualsize of the address data.The format of the address data depends on the socket namespace.  Thelength of the information is usually fixed for a given namespace, sonormally you can know exactly how much space is needed and can providethat much.  The usual practice is to allocate a place for the valueusing the proper data type for the socket's namespace, then cast itsaddress to @code{struct sockaddr *} to pass it to @code{getsockname}.The return value is @code{0} on success and @code{-1} on error.  Thefollowing @code{errno} error conditions are defined for this function:@table @code@item EBADFThe @var{socket} argument is not a valid file descriptor.@item ENOTSOCKThe descriptor @var{socket} is not a socket.@item ENOBUFSThere are not enough internal buffers available for the operation.@end table@end deftypefunYou can't read the address of a socket in the file namespace.  This isconsistent with the rest of the system; in general, there's no way tofind a file's name from a descriptor for that file.@node File Namespace@section The File Namespace@cindex file namespace, for socketsThis section describes the details of the file namespace, whosesymbolic name (required when you create a socket) is @code{PF_FILE}.@menu* Concepts: File Namespace Concepts.	What you need to understand.* Details: File Namespace Details.	Address format, symbolic names, etc.* Example: File Socket Example.		Example of creating a socket.@end menu@node File Namespace Concepts@subsection File Namespace ConceptsIn the file namespace, socket addresses are file names.  You can specifyany file name you want as the address of the socket, but you must havewrite permission on the directory containing it.  In order to connect toa socket, you must have read permission for it.  It's common to putthese files in the @file{/tmp} directory.One peculiarity of the file namespace is that the name is only used whenopening the connection; once that is over with, the address is notmeaningful and may not exist.Another peculiarity is that you cannot connect to such a socket fromanother machine--not even if the other machine shares the file systemwhich contains the name of the socket.  You can see the socket in adirectory listing, but connecting to it never succeeds.  Some programstake advantage of this, such as by asking the client to send its ownprocess ID, and using the process IDs to distinguish between clients.However, we recommend you not use this method in protocols you design,as we might someday permit connections from other machines that mountthe same file systems.  Instead, send each new client an identifyingnumber if you want it to have one.After you close a socket in the file namespace, you should delete thefile name from the file system.  Use @code{unlink} or @code{remove} todo this; see @ref{Deleting Files}.The file namespace supports just one protocol for any communicationstyle; it is protocol number @code{0}.@node File Namespace Details@subsection Details of File Namespace@pindex sys/socket.hTo create a socket in the file namespace, use the constant@code{PF_FILE} as the @var{namespace} argument to @code{socket} or@code{socketpair}.  This constant is defined in @file{sys/socket.h}.@comment sys/socket.h@comment GNU@deftypevr Macro int PF_FILEThis designates the file namespace, in which socket addresses are filenames, and its associated family of protocols.@end deftypevr@comment sys/socket.h@comment BSD@deftypevr Macro int PF_UNIXThis is a synonym for @code{PF_FILE}, for compatibility's sake.@end deftypevrThe structure for specifying socket names in the file namespace isdefined in the header file @file{sys/un.h}:@pindex sys/un.h@comment sys/un.h@comment BSD@deftp {Data Type} {struct sockaddr_un}This structure is used to specify file namespace socket addresses.  It hasthe following members:@table @code@item short int sun_familyThis identifies the address family or format of the socket address.You should store the value @code{AF_FILE} to designate the filenamespace.  @xref{Socket Addresses}.@item char sun_path[108]This is the file name to use.@strong{Incomplete:}  Why is 108 a magic number?  RMS suggests makingthis a zero-length array and tweaking the example following to use@code{alloca} to allocate an appropriate amount of storage based onthe length of the filename.@end table@end deftpYou should compute the @var{length} parameter for a socket address inthe file namespace as the sum of the size of the @code{sun_family}component and the string length (@emph{not} the allocation size!) ofthe file name string.@node File Socket Example@subsection Example of File-Namespace SocketsHere is an example showing how to create and name a socket in the filenamespace.@smallexample@include mkfsock.c.texi@end smallexample@node Internet Namespace@section The Internet Namespace@cindex Internet namespace, for socketsThis section describes the details the protocols and socket namingconventions used in the Internet namespace.To create a socket in the Internet namespace, use the symbolic name@code{PF_INET} of this namespace as the @var{namespace} argument to@code{socket} or @code{socketpair}.  This macro is defined in@file{sys/socket.h}.@pindex sys/socket.h@comment sys/socket.h@comment BSD@deftypevr Macro int PF_INETThis designates the Internet namespace and associated family ofprotocols.@end deftypevrA socket address for the Internet namespace includes the following components:@itemize @bullet@itemThe address of the machine you want to connect to.  Internet addressescan be specified in several ways; these are discussed in @ref{InternetAddress Format}, @ref{Host Addresses}, and @ref{Host Names}.@itemA port number for that machine.  @xref{Ports}.@end itemizeYou must ensure that the address and port number are represented in acanonical format called @dfn{network byte order}.  @xref{Byte Order},for information about this.@menu* Internet Address Format::     How socket addresses are specified in the                                 Internet namespace.* Host Addresses::	        All about host addresses of internet host.* Protocols Database::		Referring to protocols by name.* Ports::			Internet port numbers.* Services Database::           Ports may have symbolic names.* Byte Order::		        Different hosts may use different byte                                 ordering conventions; you need to                                 canonicalize host address and port number. * Inet Example::	        Putting it all together.@end menu@node Internet Address Format@subsection Internet Socket Address FormatIn the Internet namespace, a socket address consists of a host addressand a port on that host.  In addition, the protocol you choose serveseffectively as a part of the address because local port numbers aremeaningful only within a particular protocol.The data type for representing socket addresses in the Internet namespaceis defined in the header file @file{netinet/in.h}.@pindex netinet/in.h@comment netinet/in.h@comment BSD@deftp {Data Type} {struct sockaddr_in}This is the data type used to represent socket addresses in theInternet namespace.  It has the following members:@table @code@item short int sin_familyThis identifies the address family or format of the socket address.You should store the value of @code{AF_INET} in this member.@xref{Socket Addresses}.@item struct in_addr sin_addrThis is the Internet address of the host machine.  @xref{HostAddresses}, and @ref{Host Names}, for how to get a value to storehere.@item unsigned short int sin_portThis is the port number.  @xref{Ports}.@end table@end deftpWhen you call @code{bind} or @code{getsockname}, you should specify@code{sizeof (struct sockaddr_in)} as the @var{length} parameter ifyou are using an Internet namespace socket address.@node Host Addresses@subsection Host AddressesEach computer on the Internet has one or more @dfn{Internet addresses},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}.Each computer also has one or more @dfn{host names}, which are stringsof words separated by periods, as in @samp{churchy.gnu.ai.mit.edu}.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.@menu* Abstract Host Addresses::	What a host number consists of.* Data type: Host Address Data Type.	Data type for a host number.* Functions: Host Address Functions.	Functions to operate on them.* Names: Host Names.		Translating host names to host numbers.@end menu@node Abstract Host Addresses @subsubsection Internet Host Addresses@cindex host address, Internet@cindex Internet host address@ifinfoEach computer on the Internet has one or more Internet addresses,numbers which identify that computer among all those on the Internet.@end ifinfo@cindex network number@cindex local network address numberAn Internet host address is a number containing four bytes of data.These are divided into two parts, a @dfn{network number} and a@dfn{local network address number} within that network.  The networknumber consists of the first one, two or three bytes; the rest of thebytes are the local address.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.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.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.The Class A network 127 is reserved for loopback; you can always usethe Internet address @samp{127.0.0.1} to refer to the host machine.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.@c !!! this section could document the IN_CLASS* macros in <netinet/in.h>.@cindex standard dot notation, for Internet addresses@cindex dot notation, for Internet addressesThere are four forms of the @dfn{standard numbers-and-dots notation}for Internet addresses:@table @code@item @var{a}.@var{b}.@var{c}.@var{d}

⌨️ 快捷键说明

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