<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.11 $ -->
  <refentry id="function.stream-socket-client">
   <refnamediv>
    <refname>stream_socket_client</refname>
    <refpurpose>
     Open Internet or Unix domain socket connection
    </refpurpose> 
   </refnamediv>
   <refsect1>
    <title>Description</title>
     <methodsynopsis>
      <type>resource</type><methodname>stream_socket_client</methodname>
      <methodparam><type>string</type><parameter>remote_socket</parameter></methodparam>
      <methodparam choice="opt"><type>int</type><parameter>&amp;errno</parameter></methodparam>
      <methodparam choice="opt"><type>string</type><parameter>&amp;errstr</parameter></methodparam>
      <methodparam choice="opt"><type>float</type><parameter>timeout</parameter></methodparam>
      <methodparam choice="opt"><type>int</type><parameter>flags</parameter></methodparam>
      <methodparam choice="opt"><type>resource</type><parameter>context</parameter></methodparam>
     </methodsynopsis>
    <para>
     Initiates a stream or datagram connection to the destination specified
     by <parameter>remote_socket</parameter>.  The type of socket created
     is determined by the transport specified using standard URL formatting:
     <literal>transport://target</literal>.  For Internet Domain sockets
     (AF_INET) such as TCP and UDP, the <literal>target</literal> portion
     of the <parameter>remote_socket</parameter> parameter should consist of
     a hostname or IP address followed by a colon and a port number.  For Unix
     domain sockets, the <parameter>target</parameter> portion should point
     to the socket file on the filesystem.

     The optional <parameter>timeout</parameter> can be used to
     set a timeout in seconds for the connect system call.

     <parameter>flags</parameter> is a bitmask field which may be set to any
     combination of connection flags.  Currently the selection of connection
     flags is limited to <constant>STREAM_CLIENT_ASYNC_CONNECT</constant> and
     <constant>STREAM_CLIENT_PERSISTENT</constant>.
    </para>
    <note>
     <simpara>
      If you need to set a timeout for reading/writing data over the socket,
      use <function>stream_set_timeout</function>, as the <parameter>timeout</parameter>
      parameter to <function>stream_socket_client</function> only applies while
      connecting the socket.
     </simpara>
    </note>
    <para>
     <function>stream_socket_client</function> returns a
     stream resource which may
     be used together with the other file functions (such as
     <function>fgets</function>, <function>fgetss</function>,
     <function>fwrite</function>, <function>fclose</function>, and
     <function>feof</function>).
    </para>
    <para>
     If the call fails, it will return &false; and if the optional
     <parameter>errno</parameter> and <parameter>errstr</parameter>
     arguments are present they will be set to indicate the actual
     system level error that occurred in the system-level
     <literal>connect()</literal> call. If the value returned in
     <parameter>errno</parameter> is <literal>0</literal> and the
     function returned &false;, it is an indication that the error
     occurred before the <literal>connect()</literal> call. This is
     most likely due to a problem initializing the socket. Note that
     the <parameter>errno</parameter> and
     <parameter>errstr</parameter> arguments will always be passed by
     reference.
    </para>
    <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 <function>stream_get_transports</function>.
     See <xref linkend="transports"/> for a list of built in transports.
    </para>
    <para>
     The stream will by default be opened in blocking mode.  You can
     switch it to non-blocking mode by using
     <function>stream_set_blocking</function>.
     <example>
      <title><function>stream_socket_client</function> Example</title>
      <programlisting role="php">
<![CDATA[
<?php
$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
    while (!feof($fp)) {
        echo fgets($fp, 1024);
    }
    fclose($fp);
}
?>
]]>
      </programlisting>
     </example>
     The example below shows how to retrieve the day and time
     from the UDP service "daytime" (port 13) in your own machine.
     <example>
      <title>Using UDP connection</title>
      <programlisting role="php">
<![CDATA[
<?php
$fp = stream_socket_client("udp://127.0.0.1:13", $errno, $errstr);
if (!$fp) {
    echo "ERROR: $errno - $errstr<br />\n";
} else {
    fwrite($fp, "\n");
    echo fread($fp, 26);
    fclose($fp);
}
?>
]]>
      </programlisting>
     </example>
     <warning>
      <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 "connectionless" 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.
      </simpara>
     </warning>
    </para>
    &ipv6.brackets;
    <para>
     See also <function>stream_socket_server</function>, 
     <function>stream_set_blocking</function>,
     <function>stream_set_timeout</function>, 
     <function>stream_select</function>, 
     <function>fgets</function>,
     <function>fgetss</function>, <function>fwrite</function>,
     <function>fclose</function>, <function>feof</function>, and
     the <link linkend="ref.curl">Curl extension</link>.
    </para>
   </refsect1>
  </refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->