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

📄 library_15.html

📁 linux_c函数,linux下编程必备的
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<P>
<TT>`sys/socket.h'</TT> defines symbols starting with <SAMP>`AF_'</SAMP> for many
different kinds of networks, all or most of which are not actually
implemented.  We will document those that really work, as we receive
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.  The
prototype 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 the
address; the detailed format of the address depends on the namespace.
The first part of the address is always the format designator, which
specifies a namespace, and says that the address is in the format for
that namespace.
<P>
The return value is <CODE>0</CODE> on success and <CODE>-1</CODE> on failure.  The
following <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 the
Internet domain, only the super-user is allowed to specify a port number
in the range 0 through <CODE>IPPORT_RESERVED</CODE> minus one; 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>.)
</DL>
<P>
Additional conditions may be possible depending on the particular namespace
of 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 an
Internet 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 the
address 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 the
allocation size of <VAR>addr</VAR>, and on return it contains the actual
size of the address data.
<P>
The format of the address data depends on the socket namespace.  The
length of the information is usually fixed for a given namespace, so
normally you can know exactly how much space is needed and can provide
that much.  The usual practice is to allocate a place for the value
using the proper data type for the socket's namespace, then cast its
address 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.  The
following <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 is
consistent with the rest of the system; in general, there's no way to
find 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, whose
symbolic 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 specify
any file name you want as the address of the socket, but you must have
write permission on the directory containing it.  In order to connect to
a socket, you must have read permission for it.  It's common to put
these files in the <TT>`/tmp'</TT> directory.
<P>
One peculiarity of the file namespace is that the name is only used when
opening the connection; once that is over with, the address is not
meaningful and may not exist.
<P>
Another peculiarity is that you cannot connect to such a socket from
another machine--not even if the other machine shares the file system
which contains the name of the socket.  You can see the socket in a
directory listing, but connecting to it never succeeds.  Some programs
take advantage of this, such as by asking the client to send its own
process 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 mount
the same file systems.  Instead, send each new client an identifying
number if you want it to have one.
<P>
After you close a socket in the file namespace, you should delete the
file name from the file system.  Use <CODE>unlink</CODE> or <CODE>remove</CODE> to
do 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 communication
style; 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 file
names, 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 is
defined 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 has
the 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 file
namespace.  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 making
this a zero-length array and tweaking the example following to use
<CODE>alloca</CODE> to allocate an appropriate amount of storage based on
the length of the filename.
</DL>
<P>
You should compute the <VAR>length</VAR> parameter for a socket address in
the 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!) of
the 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 file
namespace.
<P>
<PRE>
#include &#60;stddef.h&#62;
#include &#60;stdio.h&#62;
#include &#60;errno.h&#62;
#include &#60;stdlib.h&#62;
#include &#60;sys/socket.h&#62;
#include &#60;sys/un.h&#62;

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 &#60; 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 *) &#38;name, size) &#60; 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 naming
conventions 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 of
protocols.
<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 addresses
can 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 a
canonical 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 address
and a port on that host.  In addition, the protocol you choose serves
effectively as a part of the address because local port numbers are
meaningful only within a particular protocol.
<P>
The data type for representing socket addresses in the Internet namespace
is defined in the header file <TT>`netinet/in.h'</TT>.
<A NAME="IDX933"></A>

⌨️ 快捷键说明

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