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

📄 library_15.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!-- This HTML file has been created by texi2html 1.27     from library.texinfo on 3 March 1994 --><TITLE>The GNU C Library - Sockets</TITLE><P>Go to the <A HREF="library_14.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_14.html">previous</A>, <A HREF="library_16.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_16.html">next</A> section.<P><H1><A NAME="SEC216" HREF="library_toc.html#SEC216" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC216">Sockets</A></H1><P>This chapter describes the GNU facilities for interprocesscommunication using sockets.<A NAME="IDX889"></A><A NAME="IDX890"></A><P>A <DFN>socket</DFN> 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>, <CODE>rlogin</CODE>,<CODE>ftp</CODE>, <CODE>talk</CODE>, and the other familiar network programs usesockets.<P>Not all operating systems support sockets.  In the GNU library, theheader file <TT>`sys/socket.h'</TT> exists regardless of the operatingsystem, and the socket functions always exist, but if the system doesnot really support sockets, these functions always fail.<P><STRONG>Incomplete:</STRONG> We do not currently document the facilities forbroadcast messages or for configuring Internet interfaces.<P><H2><A NAME="SEC217" HREF="library_toc.html#SEC217" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC217">Socket Concepts</A></H2><A NAME="IDX891"></A><A NAME="IDX892"></A><P>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</DFN> 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:<P><UL><A NAME="IDX893"></A><A NAME="IDX894"></A><A NAME="IDX895"></A><LI><STRONG>What are the units of data transmission?</STRONG>  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</DFN>).<P><A NAME="IDX896"></A><A NAME="IDX897"></A><LI><STRONG>Can data be lost during normal operation?</STRONG>  Some communicationstyles guarantee that all the data sent arrives in the order it wassent (barring system or network crashes); others styles occasionallylose data as a normal part of operation, and may sometimes deliverpackets more than once or in the wrong order.<P>Designing a program to use unreliable communication styles usuallyinvolves taking precautions to detect lost or misordered packets andto retransmit data as needed.<P><LI><STRONG>Is communication entirely with one partner?</STRONG>  Somecommunication styles are like a telephone call--you make a<DFN>connection</DFN> with one remote socket, and then exchange datafreely.  Other styles are like mailing letters--you specify adestination address for each message you send.</UL><A NAME="IDX898"></A><A NAME="IDX899"></A><A NAME="IDX900"></A><A NAME="IDX901"></A><P>You must also choose a <DFN>namespace</DFN> 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_'</SAMP>.A corresponding symbolic name starting with <SAMP>`AF_'</SAMP> designates theaddress format for that namespace.<A NAME="IDX902"></A><A NAME="IDX903"></A><A NAME="IDX904"></A><A NAME="IDX905"></A><P>Finally you must next choose the <DFN>protocol</DFN> 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</DFN> because of this, which is why the namespace namesstart with <SAMP>`PF_'</SAMP>.<P>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:<P><UL><LI>In order to have communication between two sockets, they must specifythe <EM>same</EM> protocol.<P><LI>Each 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.<P><LI>For each combination of style and namespace, there is a <DFN>defaultprotocol</DFN> which you can request by specifying 0 as the protocolnumber.  And that's what you should normally do--use the default.</UL><P><H2><A NAME="SEC218" HREF="library_toc.html#SEC218" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC218">Communication Styles</A></H2><P>The 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 <TT>`sys/socket.h'</TT>.<A NAME="IDX906"></A><P><A NAME="IDX907"></A><U>Macro:</U> int <B>SOCK_STREAM</B><P>The <CODE>SOCK_STREAM</CODE> style is like a pipe (see section <A HREF="library_14.html#SEC211" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_14.html#SEC211">Pipes and FIFOs</A>);it operates over a connection with a particular remote socket, andtransmits data reliably as a stream of bytes.<P>Use of this style is covered in detail in section <A HREF="library_15.html#SEC244" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC244">Using Sockets with Connections</A>.<P><A NAME="IDX908"></A><U>Macro:</U> int <B>SOCK_DGRAM</B><P>The <CODE>SOCK_DGRAM</CODE> style is used for sendingindividually-addressed packets, unreliably.  It is the diametrical opposite of <CODE>SOCK_STREAM</CODE>.<P>Each time you write data to a socket of this kind, that data becomesone packet.  Since <CODE>SOCK_DGRAM</CODE> sockets do not have connections,you must specify the recipient address with each packet.<P>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.<P>The typical use for <CODE>SOCK_DGRAM</CODE> is in situations where it isacceptible to simply resend a packet if no response is seen in areasonable amount of time.<P>See section <A HREF="library_15.html#SEC256" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC256">Datagram Socket Operations</A>, for detailed information about how to use datagramsockets.<P><A NAME="IDX909"></A><U>Macro:</U> int <B>SOCK_RAW</B><P>This style provides access to low-level network protocols andinterfaces.  Ordinary user programs usually have no need to use thisstyle.<P><H2><A NAME="SEC219" HREF="library_toc.html#SEC219" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC219">Socket Addresses</A></H2><A NAME="IDX910"></A><A NAME="IDX911"></A><A NAME="IDX912"></A><A NAME="IDX913"></A><P>The name of a socket is normally called an <DFN>address</DFN>.  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.<P>A socket newly created with the <CODE>socket</CODE> function has noaddress.  Other processes can find it for communication only if yougive it an address.  We call this <DFN>binding</DFN> the address to thesocket, and the way to do it is with the <CODE>bind</CODE> function.<P>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.<P>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</CODE> (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>).<P>The details of socket addresses vary depending on what namespace you areusing.  See section <A HREF="library_15.html#SEC223" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC223">The File Namespace</A>, or section <A HREF="library_15.html#SEC227" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC227">The Internet Namespace</A>, for specificinformation.<P>Regardless of the namespace, you use the same functions <CODE>bind</CODE> and<CODE>getsockname</CODE> to set and examine a socket's address.  Thesefunctions use a phony data type, <CODE>struct sockaddr *</CODE>, 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 *</CODE> when you pass it to<CODE>bind</CODE>.<P><H3><A NAME="SEC220" HREF="library_toc.html#SEC220" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC220">Address Formats</A></H3><P>The functions <CODE>bind</CODE> and <CODE>getsockname</CODE> use the generic datatype <CODE>struct sockaddr *</CODE> 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.<P>Thus, the usual practice is to construct an address in the propernamespace-specific type, then cast a pointer to <CODE>struct sockaddr *</CODE>when you call <CODE>bind</CODE> or <CODE>getsockname</CODE>.<P>The one piece of information that you can get from the <CODE>structsockaddr</CODE> data type is the <DFN>address format</DFN> designator which tellsyou which data type to use to understand the address fully.<A NAME="IDX914"></A><P>The symbols in this section are defined in the header file<TT>`sys/socket.h'</TT>.<P><A NAME="IDX915"></A><U>Date Type:</U> <B>struct sockaddr</B><P>The <CODE>struct sockaddr</CODE> type itself has the following members:<P><DL COMPACT><DT><CODE>short int sa_family</CODE><DD>This is the code for the address format of this address.  Itidentifies the format of the data which follows.<P><DT><CODE>char sa_data[14]</CODE><DD>This is the actual socket address data, which is format-dependent.  Itslength is also format-dependent, and may well be more than 14.  Thelength 14 of <CODE>sa_data</CODE> is essentially arbitrary.</DL><P>Each address format has a symbolic name which starts with <SAMP>`AF_'</SAMP>.Each of them corresponds to a <SAMP>`PF_'</SAMP> symbol which designates thecorresponding namespace.  Here is a list of address format names:<P><DL COMPACT><A NAME="IDX916"></A><DT><CODE>AF_FILE</CODE><DD>This designates the address format that goes with the file namespace.(<CODE>PF_FILE</CODE> is the name of that namespace.)  See section <A HREF="library_15.html#SEC225" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC225">Details of File Namespace</A>, for information about this address format.<P><A NAME="IDX917"></A><DT><CODE>AF_UNIX</CODE><DD>This is a synonym for <CODE>AF_FILE</CODE>, for compatibility.(<CODE>PF_UNIX</CODE> is likewise a synonym for <CODE>PF_FILE</CODE>.)<P><A NAME="IDX918"></A><DT><CODE>AF_INET</CODE><DD>This designates the address format that goes with the Internetnamespace.  (<CODE>PF_INET</CODE> is the name of that namespace.)See section <A HREF="library_15.html#SEC228" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC228">Internet Socket Address Format</A>.<P><A NAME="IDX919"></A><DT><CODE>AF_UNSPEC</CODE><DD>This 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.  See section <A HREF="library_15.html#SEC257" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC257">Sending Datagrams</A>.<P>The corresponding namespace designator symbol <CODE>PF_UNSPEC</CODE> existsfor completeness, but there is no reason to use it in a program.</DL><P><TT>`sys/socket.h'</TT> defines symbols starting with <SAMP>`AF_'</SAMP> for manydifferent kinds of networks, all or most of which are not actuallyimplemented.  We will document those that really work, as we receive

⌨️ 快捷键说明

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