📄 library_15.html
字号:
information about how to use them.<P><H3><A NAME="SEC221" HREF="library_toc.html#SEC221" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC221">Setting a Socket's Address</A></H3><A NAME="IDX920"></A><P>Use the <CODE>bind</CODE> function to assign an address to a socket. Theprototype for <CODE>bind</CODE> is in the header file <TT>`sys/socket.h'</TT>.For examples of use, 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 see section <A HREF="library_15.html#SEC238" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC238">Internet Socket Example</A>.<P><A NAME="IDX921"></A><U>Function:</U> int <B>bind</B> <I>(int <VAR>socket</VAR>, struct sockaddr *<VAR>addr</VAR>, size_t <VAR>length</VAR>)</I><P>The <CODE>bind</CODE> function assigns an address to the socket<VAR>socket</VAR>. The <VAR>addr</VAR> and <VAR>length</VAR> 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.<P>The return value is <CODE>0</CODE> on success and <CODE>-1</CODE> on failure. Thefollowing <CODE>errno</CODE> error conditions are defined for this function:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>socket</VAR> argument is not a valid file descriptor.<P><DT><CODE>ENOTSOCK</CODE><DD>The descriptor <VAR>socket</VAR> is not a socket.<P><DT><CODE>EADDRNOTAVAIL</CODE><DD>The specified address is not available on this machine.<P><DT><CODE>EADDRINUSE</CODE><DD>Some other socket is already using the specified address.<P><DT><CODE>EINVAL</CODE><DD>The socket <VAR>socket</VAR> already has an address.<P><DT><CODE>EACCESS</CODE><DD>You 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</CODE> minus one; seesection <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>Additional conditions may be possible depending on the particular namespaceof the socket.<P><H3><A NAME="SEC222" HREF="library_toc.html#SEC222" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC222">Reading a Socket's Address</A></H3><A NAME="IDX922"></A><P>Use the function <CODE>getsockname</CODE> to examine the address of anInternet socket. The prototype for this function is in the header file<TT>`sys/socket.h'</TT>.<P><A NAME="IDX923"></A><U>Function:</U> int <B>getsockname</B> <I>(int <VAR>socket</VAR>, struct sockaddr *<VAR>addr</VAR>, size_t *<VAR>length_ptr</VAR>)</I><P>The <CODE>getsockname</CODE> function returns information about theaddress of the socket <VAR>socket</VAR> in the locations specified by the<VAR>addr</VAR> and <VAR>length_ptr</VAR> arguments. Note that the<VAR>length_ptr</VAR> is a pointer; you should initialize it to be theallocation size of <VAR>addr</VAR>, and on return it contains the actualsize of the address data.<P>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 *</CODE> to pass it to <CODE>getsockname</CODE>.<P>The return value is <CODE>0</CODE> on success and <CODE>-1</CODE> on error. Thefollowing <CODE>errno</CODE> error conditions are defined for this function:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>socket</VAR> argument is not a valid file descriptor.<P><DT><CODE>ENOTSOCK</CODE><DD>The descriptor <VAR>socket</VAR> is not a socket.<P><DT><CODE>ENOBUFS</CODE><DD>There are not enough internal buffers available for the operation.</DL><P>You 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.<P><A NAME="IDX924"></A><H2><A NAME="SEC223" HREF="library_toc.html#SEC223" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC223">The File Namespace</A></H2><P>This section describes the details of the file namespace, whosesymbolic name (required when you create a socket) is <CODE>PF_FILE</CODE>.<P><H3><A NAME="SEC224" HREF="library_toc.html#SEC224" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC224">File Namespace Concepts</A></H3><P>In 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 <TT>`/tmp'</TT> directory.<P>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.<P>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.<P>After you close a socket in the file namespace, you should delete thefile name from the file system. Use <CODE>unlink</CODE> or <CODE>remove</CODE> todo this; see section <A HREF="library_13.html#SEC197" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC197">Deleting Files</A>.<P>The file namespace supports just one protocol for any communicationstyle; it is protocol number <CODE>0</CODE>.<P><H3><A NAME="SEC225" HREF="library_toc.html#SEC225" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC225">Details of File Namespace</A></H3><A NAME="IDX925"></A><P>To create a socket in the file namespace, use the constant<CODE>PF_FILE</CODE> as the <VAR>namespace</VAR> argument to <CODE>socket</CODE> or<CODE>socketpair</CODE>. This constant is defined in <TT>`sys/socket.h'</TT>.<P><A NAME="IDX926"></A><U>Macro:</U> int <B>PF_FILE</B><P>This designates the file namespace, in which socket addresses are filenames, and its associated family of protocols.<P><A NAME="IDX927"></A><U>Macro:</U> int <B>PF_UNIX</B><P>This is a synonym for <CODE>PF_FILE</CODE>, for compatibility's sake.<P>The structure for specifying socket names in the file namespace isdefined in the header file <TT>`sys/un.h'</TT>:<A NAME="IDX928"></A><P><A NAME="IDX929"></A><U>Data Type:</U> <B>struct sockaddr_un</B><P>This structure is used to specify file namespace socket addresses. It hasthe following members:<P><DL COMPACT><DT><CODE>short int sun_family</CODE><DD>This identifies the address family or format of the socket address.You should store the value <CODE>AF_FILE</CODE> to designate the filenamespace. 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>char sun_path[108]</CODE><DD>This is the file name to use.<P><STRONG>Incomplete:</STRONG> Why is 108 a magic number? RMS suggests makingthis a zero-length array and tweaking the example following to use<CODE>alloca</CODE> to allocate an appropriate amount of storage based onthe length of the filename.</DL><P>You should compute the <VAR>length</VAR> parameter for a socket address inthe file namespace as the sum of the size of the <CODE>sun_family</CODE>component and the string length (<EM>not</EM> the allocation size!) ofthe file name string.<P><H3><A NAME="SEC226" HREF="library_toc.html#SEC226" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC226">Example of File-Namespace Sockets</A></H3><P>Here is an example showing how to create and name a socket in the filenamespace.<P><PRE>#include <stddef.h>#include <stdio.h>#include <errno.h>#include <stdlib.h>#include <sys/socket.h>#include <sys/un.h>int make_named_socket (const char *filename){ struct sockaddr_un name; int sock; size_t size; /* Create the socket. */ sock = socket (PF_UNIX, SOCK_DGRAM, 0); if (sock < 0) { perror ("socket"); exit (EXIT_FAILURE); } /* Bind a name to the socket. */ name.sun_family = AF_FILE; strcpy (name.sun_path, filename); /* The size of the address is the offset of the start of the filename, plus its length, plus one for the terminating null byte. */ size = (offsetof (struct sockaddr_un, sun_path) + strlen (name.sun_path) + 1); if (bind (sock, (struct sockaddr *) &name, size) < 0) { perror ("bind"); exit (EXIT_FAILURE); } return sock;}</PRE><P><A NAME="IDX930"></A><H2><A NAME="SEC227" HREF="library_toc.html#SEC227" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC227">The Internet Namespace</A></H2><P>This section describes the details the protocols and socket namingconventions used in the Internet namespace.<P>To create a socket in the Internet namespace, use the symbolic name<CODE>PF_INET</CODE> of this namespace as the <VAR>namespace</VAR> argument to<CODE>socket</CODE> or <CODE>socketpair</CODE>. This macro is defined in<TT>`sys/socket.h'</TT>.<A NAME="IDX931"></A><P><A NAME="IDX932"></A><U>Macro:</U> int <B>PF_INET</B><P>This designates the Internet namespace and associated family ofprotocols.<P>A socket address for the Internet namespace includes the following components:<P><UL><LI>The address of the machine you want to connect to. Internet addressescan be specified in several ways; these are discussed in 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>, 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>.<P><LI>A port number for that machine. 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>.</UL><P>You must ensure that the address and port number are represented in acanonical format called <DFN>network byte order</DFN>. 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 information about this.<P><H3><A NAME="SEC228" HREF="library_toc.html#SEC228" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC228">Internet Socket Address Format</A></H3><P>In 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.<P>The data type for representing socket addresses in the Internet namespaceis defined in the header file <TT>`netinet/in.h'</TT>.<A NAME="IDX933"></A><P><A NAME="IDX934"></A><U>Data Type:</U> <B>struct sockaddr_in</B><P>This is the data type used to represent socket addresses in theInternet namespace. It has the following members:<P><DL COMPACT><DT><CODE>short int sin_family</CODE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -