mirror of
https://github.com/php/doc-pt_br.git
synced 2026-03-24 07:02:09 +01:00
221 lines
6.7 KiB
XML
221 lines
6.7 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 14dc7c47365f2b71f6c907a5ba5bccf42534d5a9 Maintainer: leonardolara Status: ready -->
|
|
<refentry xml:id="function.socket-create-pair" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>socket_create_pair</refname>
|
|
<refpurpose>Cria um par de soquetes indistinguíveis e os armazena em um array</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>bool</type><methodname>socket_create_pair</methodname>
|
|
<methodparam><type>int</type><parameter>domain</parameter></methodparam>
|
|
<methodparam><type>int</type><parameter>type</parameter></methodparam>
|
|
<methodparam><type>int</type><parameter>protocol</parameter></methodparam>
|
|
<methodparam><type>array</type><parameter role="reference">pair</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
<function>socket_create_pair</function> cria dois soquetes conectados e
|
|
indistinguíveis e os armazena em <parameter>pair</parameter>.
|
|
Esta função é comumente usada em IPC (Comunicação Entre Processos).
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>domain</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
O parâmetro <parameter>domain</parameter> especifica a família de
|
|
protocolos a ser usada pelo soquete. Consulte <function>socket_create</function>
|
|
para a lista completa.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>type</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
O parâmetro <parameter>type</parameter> seleciona o tipo de comunicação
|
|
a ser usado pelo soquete. Consulte <function>socket_create</function> para a
|
|
lista completa.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>protocol</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
O parâmetro <parameter>protocol</parameter> define o protocolo específico
|
|
dentro do domínio informado em <parameter>domain</parameter> a ser usado
|
|
durante a comunicação no soquete retornado. O valor adequado pode ser recuperado por
|
|
nome usando <function>getprotobyname</function>. Se
|
|
o protocolo desejado for TCP ou UDP, as constantes correspondentes
|
|
<constant>SOL_TCP</constant> e <constant>SOL_UDP</constant>
|
|
também podem ser usadas.
|
|
</para>
|
|
<para>
|
|
Consulte <function>socket_create</function> para a lista completa de protocolos
|
|
suportados.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>pair</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Referência a um array no qual serão inseridas as duas instâncias de <classname>Socket</classname>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
&return.success;
|
|
</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>pair</parameter> é agora uma referência a um array de instâncias <classname>Socket</classname>;
|
|
anteriormente, era uma referência a um array de <type>resource</type>s.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Exemplo de <function>socket_create_pair</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$sockets = array();
|
|
|
|
/* No Windows é necessário usar AF_INET */
|
|
$domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX);
|
|
|
|
/* Configura o par de soquetes */
|
|
if (socket_create_pair($domain, SOCK_STREAM, 0, $sockets) === false) {
|
|
echo "socket_create_pair falhou. Motivo: ".socket_strerror(socket_last_error());
|
|
}
|
|
/* Envia e recebe dados */
|
|
if (socket_write($sockets[0], "ABCdef123\n", strlen("ABCdef123\n")) === false) {
|
|
echo "socket_write() falhou. Motivo: ".socket_strerror(socket_last_error($sockets[0]));
|
|
}
|
|
if (($data = socket_read($sockets[1], strlen("ABCdef123\n"), PHP_BINARY_READ)) === false) {
|
|
echo "socket_read() falhou. Motivo: ".socket_strerror(socket_last_error($sockets[1]));
|
|
}
|
|
var_dump($data);
|
|
|
|
/* Fecha os soquetes */
|
|
socket_close($sockets[0]);
|
|
socket_close($sockets[1]);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Exemplo de IPC com <function>socket_create_pair</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$ary = array();
|
|
$strone = 'Mensagem do Pai.';
|
|
$strtwo = 'Mensagem do Filho.';
|
|
|
|
if (socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $ary) === false) {
|
|
echo "socket_create_pair() falhou. Motivo: ".socket_strerror(socket_last_error());
|
|
}
|
|
$pid = pcntl_fork();
|
|
if ($pid == -1) {
|
|
echo 'Não foi possível bifurcar o processo.';
|
|
} elseif ($pid) {
|
|
/* pai */
|
|
socket_close($ary[0]);
|
|
if (socket_write($ary[1], $strone, strlen($strone)) === false) {
|
|
echo "socket_write() falhou. Motivo: ".socket_strerror(socket_last_error($ary[1]));
|
|
}
|
|
if (socket_read($ary[1], strlen($strtwo), PHP_BINARY_READ) == $strtwo) {
|
|
echo "Recebida $strtwo\n";
|
|
}
|
|
socket_close($ary[1]);
|
|
} else {
|
|
/* filho */
|
|
socket_close($ary[1]);
|
|
if (socket_write($ary[0], $strtwo, strlen($strtwo)) === false) {
|
|
echo "socket_write() falhou. Motivo: ".socket_strerror(socket_last_error($ary[0]));
|
|
}
|
|
if (socket_read($ary[0], strlen($strone), PHP_BINARY_READ) == $strone) {
|
|
echo "Recebida $strone\n";
|
|
}
|
|
socket_close($ary[0]);
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>socket_create</function></member>
|
|
<member><function>socket_create_listen</function></member>
|
|
<member><function>socket_bind</function></member>
|
|
<member><function>socket_listen</function></member>
|
|
<member><function>socket_last_error</function></member>
|
|
<member><function>socket_strerror</function></member>
|
|
</simplelist>
|
|
</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:"~/.phpdoc/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
|
|
-->
|