<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.sockets.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.socket-create.php',
    1 => 'socket_create',
    2 => '创建一个套接字（通讯节点）',
  ),
  'up' => 
  array (
    0 => 'ref.sockets.php',
    1 => 'Socket 函数',
  ),
  'prev' => 
  array (
    0 => 'function.socket-connect.php',
    1 => 'socket_connect',
  ),
  'next' => 
  array (
    0 => 'function.socket-create-listen.php',
    1 => 'socket_create_listen',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/sockets/functions/socket-create.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.socket-create" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_create</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">socket_create</span> &mdash; <span class="dc-title">创建一个套接字（通讯节点）</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-create-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_create</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$domain</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$type</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$protocol</code></span>): <span class="type"><span class="type"><a href="class.socket.php" class="type Socket">Socket</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   创建并返回一个 <span class="classname"><a href="class.socket.php" class="classname">Socket</a></span> 实例，也称作一个通讯节点。一个典型的网络连接由 2 个套接字构成，一个运行在客户端，另一个运行在服务器端。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.socket-create-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">domain</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">domain</code> 参数指定哪个协议用在当前套接字上。
      </p>
      <table class="doctable table">
       <caption><strong>可用的地址/协议</strong></caption>
       
        <thead>
         <tr>
          <th>Domain</th>
          <th>描述</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.af-inet">AF_INET</a></code></strong></td>
          <td>
           IPv4 网络协议。TCP 和 UDP 都可使用此协议。
          </td>
         </tr>

         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.af-inet6">AF_INET6</a></code></strong></td>
          <td>
           IPv6 网络协议。TCP 和 UDP 都可使用此协议。
          </td>
         </tr>

         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.af-unix">AF_UNIX</a></code></strong></td>
          <td>
           本地通讯协议。具有高性能和低成本的 IPC（进程间通讯）。
          </td>
         </tr>

        </tbody>
       
      </table>

     </dd>
    
    
     <dt><code class="parameter">type</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">type</code> 参数用于选择套接字使用的类型。
      </p>
      <table class="doctable table">
       <caption><strong>可用的套接字类型</strong></caption>
       
        <thead>
         <tr>
          <th>类型</th>
          <th>描述</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.sock-stream">SOCK_STREAM</a></code></strong></td>
          <td>
           提供一个顺序化的、可靠的、全双工的、基于连接的字节流。支持数据传送流量控制机制。TCP 协议即基于这种流式套接字。
          </td>
         </tr>

         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.sock-dgram">SOCK_DGRAM</a></code></strong></td>
          <td>
           提供数据报文的支持。(无连接，不可靠、固定最大长度).UDP协议即基于这种数据报文套接字。
          </td>
         </tr>

         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.sock-seqpacket">SOCK_SEQPACKET</a></code></strong></td>
          <td>
           提供一个顺序化的、可靠的、全双工的、面向连接的、固定最大长度的数据通信；数据端通过接收每一个数据段来读取整个数据包。
          </td>
         </tr>

         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.sock-raw">SOCK_RAW</a></code></strong></td>
          <td>
           提供读取原始的网络协议。这种特殊的套接字可用于手工构建任意类型的协议。一般使用这个套接字来实现 ICMP 请求（例如 ping）。
          </td>
         </tr>

         <tr>
          <td><strong><code><a href="sockets.constants.php#constant.sock-rdm">SOCK_RDM</a></code></strong></td>
          <td>
           提供一个可靠的数据层，但不保证到达顺序。一般的操作系统都未实现此功能。
          </td>
         </tr>

        </tbody>
       
      </table>

     </dd>
    
    
     <dt><code class="parameter">protocol</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">protocol</code> 参数，是设置指定 <code class="parameter">domain</code> 
       套接字下的具体协议。这个值可以使用 <span class="function"><a href="function.getprotobyname.php" class="function">getprotobyname()</a></span>
       函数进行读取。如果所需的协议是 TCP 或 UDP，可以直接使用常量 <strong><code><a href="sockets.constants.php#constant.sol-tcp">SOL_TCP</a></code></strong> 和 <strong><code><a href="sockets.constants.php#constant.sol-udp">SOL_UDP</a></code></strong> 。
      </p>
      <table class="doctable table">
       <caption><strong>常见协议</strong></caption>
       
        <thead>
         <tr>
          <th>名称</th>
          <th>描述</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td>icmp</td>
          <td>
           互联网控制消息协议（Internet Control Message Protocol）主要用于网关和主机报告错误的数据通信。
           例如 “ping” 命令（在目前大部分的操作系统中）就是使用 ICMP 协议实现的。
          </td>
         </tr>

         <tr>
          <td>udp</td>
          <td>
           用户数据包协议（User Datagram Protocol）是一个无连接的、不可靠的、具有固定最大长度的报文协议。由于这些特性，UDP 协议拥有最小的协议开销。
          </td>
         </tr>

         <tr>
          <td>tcp</td>
          <td>
           传输控制协议（Transmission Control Protocol）是一个可靠的、基于连接的、面向数据流的全双工协议。TCP
           能够保障所有的数据包是按照其发送顺序而接收的。如果任意数据包在通讯时丢失，TCP
           将自动重发数据包直到目标主机应答已接收。因为可靠性和性能的原因，TCP 在数据传输层使用 8bit 字节边界。因此，TCP 应用程序必须允许传送部分报文的可能。
          </td>
         </tr>

        </tbody>
       
      </table>

     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.socket-create-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>socket_create()</strong></span> 正确时返回一个 <span class="classname"><a href="class.socket.php" class="classname">Socket</a></span> 实例，失败时返回
   <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。要读取错误代码，可以调用 <span class="function"><a href="function.socket-last-error.php" class="function">socket_last_error()</a></span>。这个错误代码可以通过 <span class="function"><a href="function.socket-strerror.php" class="function">socket_strerror()</a></span> 读取文字的错误说明。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.socket-create-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果使用一个无效的 <code class="parameter">domain</code> 或 <code class="parameter">type</code>，<span class="function"><strong>socket_create()</strong></span>
   会使用 <strong><code><a href="sockets.constants.php#constant.af-inet">AF_INET</a></code></strong> 和
   <strong><code><a href="sockets.constants.php#constant.sock-stream">SOCK_STREAM</a></code></strong> 替代无效参数，同时会发出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 警告信息。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.socket-create-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       创建成功时，该函数现在返回一个 <span class="classname"><a href="class.socket.php" class="classname">Socket</a></span> 实例；
       在此之前，返回的是一个 <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-create-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.socket-accept.php" class="function" rel="rdfs-seeAlso">socket_accept()</a> - 接受套接字上的连接</span></li>
    <li><span class="function"><a href="function.socket-bind.php" class="function" rel="rdfs-seeAlso">socket_bind()</a> - 给套接字绑定名字</span></li>
    <li><span class="function"><a href="function.socket-connect.php" class="function" rel="rdfs-seeAlso">socket_connect()</a> - 开启一个套接字连接</span></li>
    <li><span class="function"><a href="function.socket-listen.php" class="function" rel="rdfs-seeAlso">socket_listen()</a> - 监听套接字的连接</span></li>
    <li><span class="function"><a href="function.socket-last-error.php" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - 返回套接字上的最后一个错误</span></li>
    <li><span class="function"><a href="function.socket-strerror.php" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - 返回描述套接字错误的字符串</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>