<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.stream.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.stream-socket-client.php',
    1 => 'stream_socket_client',
    2 => 'Open Internet or Unix domain socket connection',
  ),
  'up' => 
  array (
    0 => 'ref.stream.php',
    1 => 'Stream Функції',
  ),
  'prev' => 
  array (
    0 => 'function.stream-socket-accept.php',
    1 => 'stream_socket_accept',
  ),
  'next' => 
  array (
    0 => 'function.stream-socket-enable-crypto.php',
    1 => 'stream_socket_enable_crypto',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/stream/functions/stream-socket-client.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.stream-socket-client" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_socket_client</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">stream_socket_client</span> &mdash; <span class="dc-title">Open Internet or Unix domain socket connection</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.stream-socket-client-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_socket_client</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$address</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$error_code</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter reference">&$error_message</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.float.php" class="type float">float</a></span></span> <code class="parameter">$timeout</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = <strong><code><a href="stream.constants.php#constant.stream-client-connect">STREAM_CLIENT_CONNECT</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   Initiates a stream or datagram connection to the destination specified
   by <code class="parameter">address</code>.  The type of socket created
   is determined by the transport specified using standard URL formatting:
   <code class="literal">transport://target</code>.  For Internet Domain sockets
   (AF_INET) such as TCP and UDP, the <code class="literal">target</code> portion
   of the <code class="parameter">address</code> parameter should consist of
   a hostname or IP address followed by a colon and a port number.  For Unix
   domain sockets, the <code class="literal">target</code> portion should point
   to the socket file on the filesystem.
  </p>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    The stream will by default be opened in blocking mode.  You can
    switch it to non-blocking mode by using
    <span class="function"><a href="function.stream-set-blocking.php" class="function">stream_set_blocking()</a></span>.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-socket-client-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">address</code></dt>
     <dd>
      <p class="para">
       Address to the socket to connect to.
      </p>
     </dd>
    
    
     <dt><code class="parameter">error_code</code></dt>
     <dd>
      <p class="para">
       Will be set to the system level error number if connection fails.
      </p>
     </dd>
    
    
     <dt><code class="parameter">error_message</code></dt>
     <dd>
      <p class="para">
       Will be set to the system level error message if the connection fails.
      </p>
     </dd>
    
    
     <dt><code class="parameter">timeout</code></dt>
     <dd>
      <p class="para">
       Number of seconds until the <code class="literal">connect()</code> system call
       should timeout. By default, <a href="filesystem.configuration.php#ini.default-socket-timeout" class="link">default_socket_timeout</a>
       is used.
       <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
        <span class="simpara">
         This parameter only applies when not making asynchronous
         connection attempts.
        </span>
       </p></blockquote>
       <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
        <p class="para">
         To set a timeout for reading/writing data over the socket, use the
         <span class="function"><a href="function.stream-set-timeout.php" class="function">stream_set_timeout()</a></span>, as the
         <code class="parameter">timeout</code> only applies while making connecting
         the socket.
        </p>
       </p></blockquote>
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       Bitmask field which may be set to any combination of connection flags.
       Currently the select of connection flags is limited to
       <strong><code><a href="stream.constants.php#constant.stream-client-connect">STREAM_CLIENT_CONNECT</a></code></strong> (default),
       <strong><code><a href="stream.constants.php#constant.stream-client-async-connect">STREAM_CLIENT_ASYNC_CONNECT</a></code></strong> and
       <strong><code><a href="stream.constants.php#constant.stream-client-persistent">STREAM_CLIENT_PERSISTENT</a></code></strong>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
       A valid context resource created with <span class="function"><a href="function.stream-context-create.php" class="function">stream_context_create()</a></span>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-socket-client-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   On success a stream resource is returned which may
   be used together with the other file functions (such as
   <span class="function"><a href="function.fgets.php" class="function">fgets()</a></span>, <span class="function"><a href="function.fgetss.php" class="function">fgetss()</a></span>,
   <span class="function"><a href="function.fwrite.php" class="function">fwrite()</a></span>, <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span>, and
   <span class="function"><a href="function.feof.php" class="function">feof()</a></span>), <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.stream-socket-client-errors">
  <h3 class="title">Помилки/виключення</h3>
  <p class="para">
   On failure the <code class="parameter">error_code</code> and
   <code class="parameter">error_message</code> arguments will be populated with the actual
   system level error that occurred in the system-level
   <code class="literal">connect()</code> call. If the value returned in
   <code class="parameter">error_code</code> is <code class="literal">0</code> and the
   function returned <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, it is an indication that the error
   occurred before the <code class="literal">connect()</code> call. This is
   most likely due to a problem initializing the socket. Note that
   the <code class="parameter">error_code</code> and
   <code class="parameter">error_message</code> arguments will always be passed by
   reference.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.stream-socket-client-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>
       <code class="parameter">timeout</code> and <code class="parameter">context</code> are now nullable.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>

 
 <div class="refsect1 examples" id="refsect1-function.stream-socket-client-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="stream-socket-client.example.basic">
    <p><strong>Приклад #1 <span class="function"><strong>stream_socket_client()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_client</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://www.example.com:80"</span><span style="color: #007700">, </span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">, </span><span style="color: #0000BB">30</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)&lt;br /&gt;\n"</span><span style="color: #007700">;<br />} else {<br />    </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n"</span><span style="color: #007700">);<br />    while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)) {<br />        echo </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">1024</span><span style="color: #007700">);<br />    }<br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="stream-socket-client.example.daytime">
    <p><strong>Приклад #2 Using UDP connection</strong></p>
    <div class="example-contents"><p>
     Retrieving the day and time from the UDP service &quot;daytime&quot; (port 13)
     on localhost.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_client</span><span style="color: #007700">(</span><span style="color: #DD0000">"udp://127.0.0.1:13"</span><span style="color: #007700">, </span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"ERROR: </span><span style="color: #0000BB">$errno</span><span style="color: #DD0000"> - </span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&lt;br /&gt;\n"</span><span style="color: #007700">;<br />} else {<br />    </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br />    echo </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">26</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.stream-socket-client-notes">
  <h3 class="title">Примітки</h3>
  <div class="warning"><strong class="warning">Увага</strong>
   <p class="simpara">
     UDP sockets will sometimes appear to have opened without an error,
     even if the remote host is unreachable.  The error will only
     become apparent when you read or write data to/from the socket.
     The reason for this is because UDP is a &quot;connectionless&quot; protocol,
     which means that the operating system does not try to establish
     a link for the socket until it actually needs to send or receive data.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: <span class="simpara">Вказуючи числову
IPv6-адресу (напр. <code class="literal">fe80::1</code>), необхідно взяти IP-адресу в
квадратні дужки, наприклад,
<code class="literal">tcp://[fe80::1]:80</code>.</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    Depending on the environment, the Unix domain or the optional
    connect timeout may not be available.  A list of available
    transports can be retrieved using <span class="function"><a href="function.stream-get-transports.php" class="function">stream_get_transports()</a></span>.
    See <a href="transports.php" class="xref">Перелік підтримуваних транспортних протоколів у сокетах</a> for a list of built in transports.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-socket-client-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.stream-socket-server.php" class="function" rel="rdfs-seeAlso">stream_socket_server()</a> - Create an Internet or Unix domain server socket</span></li>
    <li><span class="function"><a href="function.stream-set-blocking.php" class="function" rel="rdfs-seeAlso">stream_set_blocking()</a> - Set blocking/non-blocking mode on a stream</span></li>
    <li><span class="function"><a href="function.stream-set-timeout.php" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
    <li><span class="function"><a href="function.stream-select.php" class="function" rel="rdfs-seeAlso">stream_select()</a> - Runs the equivalent of the select() system call on the given
   arrays of streams with a timeout specified by seconds and microseconds</span></li>
    <li><span class="function"><a href="function.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - Gets line from file pointer</span></li>
    <li><span class="function"><a href="function.fgetss.php" class="function" rel="rdfs-seeAlso">fgetss()</a> - Gets line from file pointer and strip HTML tags</span></li>
    <li><span class="function"><a href="function.fwrite.php" class="function" rel="rdfs-seeAlso">fwrite()</a> - Binary-safe file write</span></li>
    <li><span class="function"><a href="function.fclose.php" class="function" rel="rdfs-seeAlso">fclose()</a> - Closes an open file pointer</span></li>
    <li><span class="function"><a href="function.feof.php" class="function" rel="rdfs-seeAlso">feof()</a> - Tests for end-of-file on a file pointer</span></li>
    <li><a href="ref.curl.php" class="xref">Функції cURL</a></li>
   </ul>
  </p>
 </div>


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