📄 socket.texi
字号:
@node Sockets, Low-Level Terminal Interface, Pipes and FIFOs, Top@chapter SocketsThis chapter describes the GNU facilities for interprocesscommunication using sockets.@cindex socket@cindex interprocess communication, with socketsA @dfn{socket} is a generalized interprocess communication channel.Like a pipe, a socket is represented as a file descriptor. But,unlike pipes, sockets support communication between unrelatedprocesses, and even between processes running on different machinesthat communicate over a network. Sockets are the primary means ofcommunicating with other machines; @code{telnet}, @code{rlogin},@code{ftp}, @code{talk}, and the other familiar network programs usesockets.Not all operating systems support sockets. In the GNU library, theheader file @file{sys/socket.h} exists regardless of the operatingsystem, and the socket functions always exist, but if the system doesnot really support sockets, these functions always fail.@strong{Incomplete:} We do not currently document the facilities forbroadcast messages or for configuring Internet interfaces.@menu* Socket Concepts:: Basic concepts you need to know about.* Communication Styles::Stream communication, datagrams, and other styles.* Socket Addresses:: How socket names (``addresses'') work.* File Namespace:: Details about the file namespace.* Internet Namespace:: Details about the Internet namespace.* Misc Namespaces:: Other namespaces not documented fully here.* Open/Close Sockets:: Creating sockets and destroying them.* Connections:: Operations on sockets with connection state.* Datagrams:: Operations on datagram sockets.* Inetd:: Inetd is a daemon that starts servers on request. The most convenient way to write a server is to make it work with Inetd.* Socket Options:: Miscellaneous low-level socket options.* Networks Database:: Accessing the database of network names.@end menu@node Socket Concepts@section Socket Concepts@cindex communication style (of a socket)@cindex style of communication (of a socket)When you create a socket, you must specify the style of communicationyou want to use and the type of protocol that should implement it.The @dfn{communication style} of a socket defines the user-levelsemantics of sending and receiving data on the socket. Choosing acommunication style specifies the answers to questions such as these:@itemize @bullet@item@cindex packet@cindex byte stream@cindex stream (sockets)@strong{What are the units of data transmission?} Some communicationstyles regard the data as a sequence of bytes, with no largerstructure; others group the bytes into records (which are known inthis context as @dfn{packets}).@item@cindex loss of data on sockets@cindex data loss on sockets@strong{Can data be lost during normal operation?} Some communicationstyles guarantee that all the data sent arrives in the order it wassent (barring system or network crashes); other styles occasionallylose data as a normal part of operation, and may sometimes deliverpackets more than once or in the wrong order.Designing a program to use unreliable communication styles usuallyinvolves taking precautions to detect lost or misordered packets andto retransmit data as needed.@item@strong{Is communication entirely with one partner?} Somecommunication styles are like a telephone call---you make a@dfn{connection} with one remote socket, and then exchange datafreely. Other styles are like mailing letters---you specify adestination address for each message you send.@end itemize@cindex namespace (of socket)@cindex domain (of socket)@cindex socket namespace@cindex socket domainYou must also choose a @dfn{namespace} for naming the socket. A socketname (``address'') is meaningful only in the context of a particularnamespace. In fact, even the data type to use for a socket name maydepend on the namespace. Namespaces are also called ``domains'', but weavoid that word as it can be confused with other usage of the sameterm. Each namespace has a symbolic name that starts with @samp{PF_}.A corresponding symbolic name starting with @samp{AF_} designates theaddress format for that namespace.@cindex network protocol@cindex protocol (of socket)@cindex socket protocol@cindex protocol familyFinally you must choose the @dfn{protocol} to carry out thecommunication. The protocol determines what low-level mechanism is usedto transmit and receive data. Each protocol is valid for a particularnamespace and communication style; a namespace is sometimes called a@dfn{protocol family} because of this, which is why the namespace namesstart with @samp{PF_}.The rules of a protocol apply to the data passing between two programs,perhaps on different computers; most of these rules are handled by theoperating system, and you need not know about them. What you do need toknow about protocols is this:@itemize @bullet@itemIn order to have communication between two sockets, they must specifythe @emph{same} protocol.@itemEach protocol is meaningful with particular style/namespacecombinations and cannot be used with inappropriate combinations. Forexample, the TCP protocol fits only the byte stream style ofcommunication and the Internet namespace.@itemFor each combination of style and namespace, there is a @dfn{defaultprotocol} which you can request by specifying 0 as the protocolnumber. And that's what you should normally do---use the default.@end itemize@node Communication Styles@section Communication StylesThe GNU library includes support for several different kinds of sockets,each with different characteristics. This section describes thesupported socket types. The symbolic constants listed here aredefined in @file{sys/socket.h}.@pindex sys/socket.h@comment sys/socket.h@comment BSD@deftypevr Macro int SOCK_STREAMThe @code{SOCK_STREAM} style is like a pipe (@pxref{Pipes and FIFOs});it operates over a connection with a particular remote socket, andtransmits data reliably as a stream of bytes.Use of this style is covered in detail in @ref{Connections}.@end deftypevr@comment sys/socket.h@comment BSD@deftypevr Macro int SOCK_DGRAMThe @code{SOCK_DGRAM} style is used for sendingindividually-addressed packets, unreliably. It is the diametrical opposite of @code{SOCK_STREAM}.Each time you write data to a socket of this kind, that data becomesone packet. Since @code{SOCK_DGRAM} sockets do not have connections,you must specify the recipient address with each packet.The only guarantee that the system makes about your requests totransmit data is that it will try its best to deliver each packet yousend. It may succeed with the sixth packet after failing with thefourth and fifth packets; the seventh packet may arrive before thesixth, and may arrive a second time after the sixth.The typical use for @code{SOCK_DGRAM} is in situations where it isacceptable to simply resend a packet if no response is seen in areasonable amount of time.@xref{Datagrams}, for detailed information about how to use datagramsockets.@end deftypevr@ignore@c This appears to be only for the NS domain, which we aren't@c discussing and probably won't support either.@comment sys/socket.h@comment BSD@deftypevr Macro int SOCK_SEQPACKETThis style is like @code{SOCK_STREAM} except that the data isstructured into packets.A program that receives data over a @code{SOCK_SEQPACKET} socketshould be prepared to read the entire message packet in a single callto @code{read}; if it only reads part of the message, the remainder ofthe message is simply discarded instead of being available forsubsequent calls to @code{read}.Many protocols do not support this communication style.@end deftypevr@end ignore@ignore@comment sys/socket.h@comment BSD@deftypevr Macro int SOCK_RDMThis style is a reliable version of @code{SOCK_DGRAM}: it sendsindividually addressed packets, but guarantees that each packet sentarrives exactly once.@strong{Warning:} It is not clear this is actually supportedby any operating system.@end deftypevr@end ignore@comment sys/socket.h@comment BSD@deftypevr Macro int SOCK_RAWThis style provides access to low-level network protocols andinterfaces. Ordinary user programs usually have no need to use thisstyle.@end deftypevr@node Socket Addresses@section Socket Addresses@cindex address of socket@cindex name of socket@cindex binding a socket address@cindex socket address (name) bindingThe name of a socket is normally called an @dfn{address}. Thefunctions and symbols for dealing with socket addresses were namedinconsistently, sometimes using the term ``name'' and sometimes using``address''. You can regard these terms as synonymous where socketsare concerned.A socket newly created with the @code{socket} function has noaddress. Other processes can find it for communication only if yougive it an address. We call this @dfn{binding} the address to thesocket, and the way to do it is with the @code{bind} function.You need be concerned with the address of a socket if other processesare to find it and start communicating with it. You can specify anaddress for other sockets, but this is usually pointless; the first timeyou send data from a socket, or use it to initiate a connection, thesystem assigns an address automatically if you have not specified one.Occasionally a client needs to specify an address because the serverdiscriminates based on addresses; for example, the rsh and rloginprotocols look at the client's socket address and don't bypass passwordchecking unless it is less than @code{IPPORT_RESERVED} (@pxref{Ports}).The details of socket addresses vary depending on what namespace you areusing. @xref{File Namespace}, or @ref{Internet Namespace}, for specificinformation.Regardless of the namespace, you use the same functions @code{bind} and@code{getsockname} to set and examine a socket's address. Thesefunctions use a phony data type, @code{struct sockaddr *}, to accept theaddress. In practice, the address lives in a structure of some otherdata type appropriate to the address format you are using, but you castits address to @code{struct sockaddr *} when you pass it to@code{bind}.@menu* Address Formats:: About @code{struct sockaddr}.* Setting Address:: Binding an address to a socket.* Reading Address:: Reading the address of a socket.@end menu@node Address Formats@subsection Address FormatsThe functions @code{bind} and @code{getsockname} use the generic datatype @code{struct sockaddr *} to represent a pointer to a socketaddress. You can't use this data type effectively to interpret anaddress or construct one; for that, you must use the proper data typefor the socket's namespace.Thus, the usual practice is to construct an address in the propernamespace-specific type, then cast a pointer to @code{struct sockaddr *}when you call @code{bind} or @code{getsockname}.The one piece of information that you can get from the @code{structsockaddr} data type is the @dfn{address format} designator which tellsyou which data type to use to understand the address fully.@pindex sys/socket.hThe symbols in this section are defined in the header file@file{sys/socket.h}.@comment sys/socket.h@comment BSD@deftp {Date Type} {struct sockaddr}The @code{struct sockaddr} type itself has the following members:@table @code@item short int sa_familyThis is the code for the address format of this address. Itidentifies the format of the data which follows.@item char sa_data[14]This is the actual socket address data, which is format-dependent. Itslength also depends on the format, and may well be more than 14. Thelength 14 of @code{sa_data} is essentially arbitrary.@end table@end deftpEach address format has a symbolic name which starts with @samp{AF_}.Each of them corresponds to a @samp{PF_} symbol which designates thecorresponding namespace. Here is a list of address format names:@table @code@comment sys/socket.h@comment GNU@item AF_FILE@vindex AF_FILEThis designates the address format that goes with the file namespace.(@code{PF_FILE} is the name of that namespace.) @xref{File NamespaceDetails}, for information about this address format.@comment sys/socket.h@comment BSD@item AF_UNIX@vindex AF_UNIXThis is a synonym for @code{AF_FILE}, for compatibility.(@code{PF_UNIX} is likewise a synonym for @code{PF_FILE}.)@comment sys/socket.h@comment BSD@item AF_INET@vindex AF_INETThis designates the address format that goes with the Internetnamespace. (@code{PF_INET} is the name of that namespace.)@xref{Internet Address Format}.@comment sys/socket.h@comment BSD@item AF_UNSPEC@vindex AF_UNSPECThis designates no particular address format. It is used only in rarecases, such as to clear out the default destination address of a``connected'' datagram socket. @xref{Sending Datagrams}.The corresponding namespace designator symbol @code{PF_UNSPEC} existsfor completeness, but there is no reason to use it in a program.@end table@file{sys/socket.h} defines symbols starting with @samp{AF_} for manydifferent kinds of networks, all or most of which are not actuallyimplemented. We will document those that really work, as we receiveinformation about how to use them.@node Setting Address@subsection Setting the Address of a Socket@pindex sys/socket.hUse the @code{bind} function to assign an address to a socket. Theprototype for @code{bind} is in the header file @file{sys/socket.h}.For examples of use, see @ref{File Namespace}, or see @ref{Inet Example}.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -