mirror of
https://github.com/php/doc-es.git
synced 2026-03-24 07:22:16 +01:00
* Fix typo add ref warn.deprecated.feature-8-1-0 * Remove <para> in invalid places * Fix language snippets
256 lines
8.7 KiB
XML
256 lines
8.7 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 181e9c5572ed04ed712b8d7f858f61a94647c6ac Maintainer: PhilDaiguille Status: ready -->
|
|
<!-- Reviewed: no Maintainer: Marqitos -->
|
|
<refentry xml:id="function.stream-socket-client" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>stream_socket_client</refname>
|
|
<refpurpose>Abre una conexión de socket de Internet o Unix</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type class="union"><type>resource</type><type>false</type></type><methodname>stream_socket_client</methodname>
|
|
<methodparam><type>string</type><parameter>address</parameter></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter role="reference">error_code</parameter><initializer>&null;</initializer></methodparam>
|
|
<methodparam choice="opt"><type>string</type><parameter role="reference">error_message</parameter><initializer>&null;</initializer></methodparam>
|
|
<methodparam choice="opt"><type class="union"><type>float</type><type>null</type></type><parameter>timeout</parameter><initializer>&null;</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer><constant>STREAM_CLIENT_CONNECT</constant></initializer></methodparam>
|
|
<methodparam choice="opt"><type class="union"><type>resource</type><type>null</type></type><parameter>context</parameter><initializer>&null;</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
Inicia un flujo o una conexión de datagrama con el destino
|
|
<parameter>address</parameter>. El tipo de socket creado se determina
|
|
por el transporte especificado con el formato URL siguiente:
|
|
<literal>transport://target</literal>. Para un socket de Internet,
|
|
(AF_INET) como TCP y UDP, el <literal>objetivo</literal>
|
|
de <parameter>address</parameter> será una dirección IP o un nombre de host.
|
|
Para un socket Unix, el <literal>objetivo</literal> debe ser un fichero de
|
|
socket del sistema.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
El flujo se abrirá por omisión en modo bloqueante. Puede pasarse
|
|
a modo no bloqueante utilizando la función
|
|
<function>stream_set_blocking</function>.
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>address</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
La dirección del socket.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>error_code</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Contendrá el número del error del sistema si la conexión falla.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>error_message</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Contendrá el mensaje del error del sistema si la conexión falla.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>timeout</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Tiempo límite, en segundos, para la llamada al sistema
|
|
<literal>connect()</literal>.
|
|
Por omisión, se utiliza <link linkend="ini.default-socket-timeout">default_socket_timeout</link>.
|
|
<note>
|
|
<simpara>
|
|
Este parámetro solo se aplica para conexiones
|
|
que no son asíncronas.
|
|
</simpara>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
Para definir un tiempo límite al leer/escribir
|
|
datos a través de un socket, utilice la función
|
|
<function>stream_set_timeout</function>, ya que el parámetro
|
|
<parameter>timeout</parameter> solo se aplica durante la conexión
|
|
al socket.
|
|
</para>
|
|
</note>
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>flags</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Campo de bits que puede ser la combinación de cualquier opción
|
|
de conexión. Actualmente, los valores posibles para estas opciones son
|
|
<constant>STREAM_CLIENT_CONNECT</constant> (por omisión),
|
|
<constant>STREAM_CLIENT_ASYNC_CONNECT</constant> y
|
|
<constant>STREAM_CLIENT_PERSISTENT</constant>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>context</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Un recurso de contexto válido, creado por la función
|
|
<function>stream_context_create</function>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
En caso de éxito, se devuelve un recurso de flujo, que puede ser utilizado
|
|
con otras funciones de ficheros, como
|
|
<function>fgets</function>, <function>fgetss</function>,
|
|
<function>fwrite</function>, <function>fclose</function>, y
|
|
<function>feof</function>, &false; si ocurre un error.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="errors">
|
|
&reftitle.errors;
|
|
<para>
|
|
Si la llamada falla, <function>stream_socket_client</function> devolverá
|
|
&false; y si los parámetros opcionales <parameter>error_code</parameter> y
|
|
<parameter>error_message</parameter> son proporcionados, recibirán el error
|
|
exacto que ocurrió en el sistema durante la llamada a
|
|
<literal>connect()</literal>. Si el valor devuelto en
|
|
<parameter>error_code</parameter> es <literal>0</literal> y la función
|
|
ha devuelto &false;, es una indicación de que el error ocurrió
|
|
antes de la llamada a <literal>connect()</literal>. Esto es probablemente debido
|
|
a un problema de inicialización del socket. Tenga en cuenta que
|
|
<parameter>error_code</parameter> y <parameter>error_message</parameter>
|
|
deben ser pasados por referencia.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>8.0.0</entry>
|
|
<entry>
|
|
<parameter>timeout</parameter> y <parameter>context</parameter> ahora pueden ser nulos.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example xml:id="stream-socket-client.example.basic">
|
|
<title>Ejemplo con <function>stream_socket_client</function></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>
|
|
<example xml:id="stream-socket-client.example.daytime">
|
|
<title>Uso de conexiones UDP</title>
|
|
<para>
|
|
Lee la fecha y hora en un servicio UDP de tipo
|
|
"<literal>daytime</literal>" (puerto 13) en su propia máquina:
|
|
</para>
|
|
<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>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
<warning>
|
|
<simpara>
|
|
Los sockets UDP a veces parecerán abrirse sin error, incluso si el host
|
|
remoto no es accesible. Este error solo se hará aparente
|
|
cuando se intente leer o escribir datos con este
|
|
socket. La razón es que UDP es un protocolo sin conexión,
|
|
lo que significa que el sistema operativo no tiene que establecer
|
|
un enlace para el socket, hasta que comience a intercambiar datos.
|
|
</simpara>
|
|
</warning>
|
|
&ipv6.brackets;
|
|
<note>
|
|
<para>
|
|
Según su entorno, los sockets Unix o el tiempo límite
|
|
pueden no estar disponibles. Una lista de los transportes disponibles
|
|
en el sistema es accesible mediante <function>stream_get_transports</function>.
|
|
Consulte <xref linkend="transports"/> para una lista de transportes nativos.
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>stream_socket_server</function></member>
|
|
<member><function>stream_set_blocking</function></member>
|
|
<member><function>stream_set_timeout</function></member>
|
|
<member><function>stream_select</function></member>
|
|
<member><function>fgets</function></member>
|
|
<member><function>fgetss</function></member>
|
|
<member><function>fwrite</function></member>
|
|
<member><function>fclose</function></member>
|
|
<member><function>feof</function></member>
|
|
<member><xref linkend="ref.curl"/></member>
|
|
</simplelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
</refentry>
|