Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 12091

php-manual-pt_BR-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Funções de Socket</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Manual do PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Referência das Funções"
HREF="funcref.html"><LINK
REL="PREVIOUS"
TITLE="use_soap_error_handler"
HREF="function.use-soap-error-handler.html"><LINK
REL="NEXT"
TITLE="socket_accept"
HREF="function.socket-accept.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="reference"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Manual do PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.use-soap-error-handler.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.socket-accept.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.sockets"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>CLIV. Funções de Socket</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN207823"
></A
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="sockets.intro"
>Introdução</A
></H1
><P
>&#13;     A extensão socket implementa uma interface de baixo nível para funções de comunicação
     sockets baseadas no popular BSD sockets, disponibilizando acionar
     como um servidor socket ou como um cliente.
    </P
><P
>&#13;     Para saber mais informações sobre sockets cliente-servidor, veja
     <A
HREF="function.stream-socket-client.html"
><B
CLASS="function"
>stream_socket_client()</B
></A
>, 
     <A
HREF="function.stream-socket-server.html"
><B
CLASS="function"
>stream_socket_server()</B
></A
>,
     <A
HREF="function.fsockopen.html"
><B
CLASS="function"
>fsockopen()</B
></A
>, e
     <A
HREF="function.pfsockopen.html"
><B
CLASS="function"
>pfsockopen()</B
></A
>.
    </P
><P
>&#13;     Quando usando estas funções, é importante relembrar que enquanto
     muitas delas tem nomes idênticos equivalentes em C, eles
     tem frequentemente declarações diferentes. Por favor tenha certeza lendo as descrições
     para evitar confusão.
    </P
><P
>&#13;     Aqueles não familiarizados com programação usando socket podem encontrar
     muito material útil nas "man pages" de Unix, e há uma infinidade de
     tutoriais com informações usando socket em C na web, muito dos quais
     podem ser aplicados, com pequenas modificações, para programação usando socket
     em PHP. O <A
HREF="http://www.developerweb.net/sock-faq/"
TARGET="_top"
>UNIX Socket
     FAQ</A
> pode ser um bom começo.
    </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Atenção</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>Este módulo é
 <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>EXPERIMENTAL</I
></SPAN
>. Isso quer dizer que o comportamento neste módulo ---
 incluindo suas funções e seus nomes, e TUDO mais que está documentado
 sobre esse módulo --- poderá mudar em futuras versões do PHP, SEM QUALQUER NOTIFICAÇÃO.
 Esteja avisado, e use este módulo por sua própria conta e risco.</P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="sockets.requirements"
>Dependências</A
></H1
><P
>Nenhuma biblioteca externa é necessária para compilar esta extensão.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="sockets.installation"
>Instalação</A
></H1
><P
>&#13;  As funções de socket descritas aqui fazem parte de uma extensão do PHP
  que deve ser habilitada na hora de compilar adicionando a opção <CODE
CLASS="option"
>--enable-sockets</CODE
> à linha de comando
  <B
CLASS="command"
>configure</B
>.
 </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>O suporte ao IPv6 foi acrescentado no PHP 5.0.0.</P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="sockets.configuration"
>Configurações em execução</A
></H1
><P
>Esta extensão não define nenhum parâmetro de configuração no <TT
CLASS="filename"
>php.ini</TT
>.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="sockets.resources"
>Tipos Resource</A
></H1
><P
>Esta extensão não possui nenhum tipo resource.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="sockets.constants"
>Constantes pré-definidas</A
></H1
><P
>&#13;As contantes abaixo são definidas por esta extensão e somente
 estarão disponíveis quando a extensão foi compilada com o PHP
 ou carregada dinamicamente durante a execução.
</P
><P
></P
><DIV
CLASS="variablelist"
><DL
><DT
><TT
CLASS="constant"
><B
>AF_UNIX</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>AF_INET</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>AF_INET6</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SOCK_STREAM</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SOCK_DGRAM</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SOCK_RAW</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SOCK_SEQPACKET</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SOCK_RDM</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>MSG_OOB</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>MSG_WAITALL</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>MSG_PEEK</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>MSG_DONTROUTE</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_DEBUG</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_REUSEADDR</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_KEEPALIVE</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_DONTROUTE</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_LINGER</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_BROADCAST</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_OOBINLINE</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_SNDBUF</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_RCVBUF</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_SNDLOWAT</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_RCVLOWAT</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_SNDTIMEO</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_RCVTIMEO</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_TYPE</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SO_ERROR</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SOL_SOCKET</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>PHP_NORMAL_READ</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>PHP_BINARY_READ</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SOL_TCP</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
><DT
><TT
CLASS="constant"
><B
>SOL_UDP</B
></TT
> 
    (<A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>)</DT
><DD
><P
>&#13;     
    </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="sockets.errors"
>Socket Errors</A
></H1
><P
>&#13;     A extensão socket foi escrita para disponibilizar uma interface útil e
     poderosa para sockets BSD. Tem-se notado que as funções funcionam igualmente
     bem em Win32 e Unix. Quase todas as funções de sockets
     devem falhar sob certas condições e portanto emite uma mensagem 
     <TT
CLASS="constant"
><B
>E_WARNING</B
></TT
> descrevendo o erro. De vez em quando isso
     não acontece para desespero do desenvolvedor. Por exemplo a função
     <A
HREF="function.socket-read.html"
><B
CLASS="function"
>socket_read()</B
></A
> deve de repente emitir uma mensagem
     <TT
CLASS="constant"
><B
>E_WARNING</B
></TT
> porque a conexão foi quebrada inesperadamente.
     É comum esconder as mensagens de warning com o operador
     <TT
CLASS="literal"
>@</TT
> e pegar o código de erro dentro da aplicação
     com a função <A
HREF="function.socket-last-error.html"
><B
CLASS="function"
>socket_last_error()</B
></A
>. Você
     deve chamar a função <A
HREF="function.socket-strerror.html"
><B
CLASS="function"
>socket_strerror()</B
></A
> com esse código de erro
     para retornar uma string descrevendo o erro. Veja sua descrição para mais
     informações.
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
      A mensagem de <TT
CLASS="constant"
><B
>E_WARNING</B
></TT
> gerada pela extensão socket
      estão em inglês dificilmente a mensagem de erro retornada ira aparecer correto
      dependendo da localidade padrão (<TT
CLASS="constant"
><B
>LC_MESSAGES</B
></TT
>):
      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
Warning - socket_bind() unable to bind address [98]: Die Adresse wird bereits verwendet</font>
</code></TD
></TR
></TABLE
>
     </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="sockets.examples"
>Exemplos</A
></H1
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN208068"
></A
><P
><B
>Exemplo 1. Exemplo de socket: Simples servidor TCP/IP</B
></P
><P
>&#13;       Este exemplo mostra uma simples troca de informações do servidor. Alterar as variáveis
       <CODE
CLASS="varname"
>address</CODE
> e <CODE
CLASS="varname"
>port</CODE
>
       para o conjunto da sua configuração e execução. Você deve então conectar
       o servidor com uma linha de comando similiar a: <B
CLASS="command"
>telnet 192.168.1.53
       10000</B
> (onde o endereço e a porta partem da sua configuração). Algo que você        digitar então irá sair no lado do servidor, e mostrar isso para você. 
      Para desconectar, digite 'quit'.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
#!/usr/local/bin/php -q<br /><font color="#0000BB">&lt;?php<br />error_reporting </font><font color="#007700">(</font><font color="#0000BB">E_ALL</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Allow the script to hang around waiting for connections. */<br /></font><font color="#0000BB">set_time_limit </font><font color="#007700">(</font><font color="#0000BB">0</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Turn on implicit output flushing so we see what we're getting<br /> * as it comes in. */<br /></font><font color="#0000BB">ob_implicit_flush </font><font color="#007700">();<br /><br /></font><font color="#0000BB">$address </font><font color="#007700">= </font><font color="#DD0000">'192.168.1.53'</font><font color="#007700">;<br /></font><font color="#0000BB">$port </font><font color="#007700">= </font><font color="#0000BB">10000</font><font color="#007700">;<br /><br />if ((</font><font color="#0000BB">$sock </font><font color="#007700">= </font><font color="#0000BB">socket_create </font><font color="#007700">(</font><font color="#0000BB">AF_INET</font><font color="#007700">, </font><font color="#0000BB">SOCK_STREAM</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">)) &lt; </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"socket_create() failed: reason: " </font><font color="#007700">. </font><font color="#0000BB">socket_strerror </font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br />if ((</font><font color="#0000BB">$ret </font><font color="#007700">= </font><font color="#0000BB">socket_bind </font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">, </font><font color="#0000BB">$address</font><font color="#007700">, </font><font color="#0000BB">$port</font><font color="#007700">)) &lt; </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"socket_bind() failed: reason: " </font><font color="#007700">. </font><font color="#0000BB">socket_strerror </font><font color="#007700">(</font><font color="#0000BB">$ret</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br />if ((</font><font color="#0000BB">$ret </font><font color="#007700">= </font><font color="#0000BB">socket_listen </font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">, </font><font color="#0000BB">5</font><font color="#007700">)) &lt; </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"socket_listen() failed: reason: " </font><font color="#007700">. </font><font color="#0000BB">socket_strerror </font><font color="#007700">(</font><font color="#0000BB">$ret</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br />do {<br />&nbsp;&nbsp;&nbsp;&nbsp;if ((</font><font color="#0000BB">$msgsock </font><font color="#007700">= </font><font color="#0000BB">socket_accept</font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">)) &lt; </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"socket_accept() failed: reason: " </font><font color="#007700">. </font><font color="#0000BB">socket_strerror </font><font color="#007700">(</font><font color="#0000BB">$msgsock</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Send instructions. */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$msg </font><font color="#007700">= </font><font color="#DD0000">"\nWelcome to the PHP Test Server. \n" </font><font color="#007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"To quit, type 'quit'. To shut down the server type 'shutdown'.\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">socket_write</font><font color="#007700">(</font><font color="#0000BB">$msgsock</font><font color="#007700">, </font><font color="#0000BB">$msg</font><font color="#007700">, </font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$msg</font><font color="#007700">));<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;do {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">FALSE </font><font color="#007700">=== (</font><font color="#0000BB">$buf </font><font color="#007700">= </font><font color="#0000BB">socket_read </font><font color="#007700">(</font><font color="#0000BB">$msgsock</font><font color="#007700">, </font><font color="#0000BB">2048</font><font color="#007700">, </font><font color="#0000BB">PHP_NORMAL_READ</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"socket_read() failed: reason: " </font><font color="#007700">. </font><font color="#0000BB">socket_strerror </font><font color="#007700">(</font><font color="#0000BB">$ret</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break </font><font color="#0000BB">2</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!</font><font color="#0000BB">$buf </font><font color="#007700">= </font><font color="#0000BB">trim </font><font color="#007700">(</font><font color="#0000BB">$buf</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$buf </font><font color="#007700">== </font><font color="#DD0000">'quit'</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$buf </font><font color="#007700">== </font><font color="#DD0000">'shutdown'</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">socket_close </font><font color="#007700">(</font><font color="#0000BB">$msgsock</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break </font><font color="#0000BB">2</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$talkback </font><font color="#007700">= </font><font color="#DD0000">"PHP: You said '$buf'.</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">socket_write </font><font color="#007700">(</font><font color="#0000BB">$msgsock</font><font color="#007700">, </font><font color="#0000BB">$talkback</font><font color="#007700">, </font><font color="#0000BB">strlen </font><font color="#007700">(</font><font color="#0000BB">$talkback</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"$buf</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;} while (</font><font color="#0000BB">true</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">socket_close </font><font color="#007700">(</font><font color="#0000BB">$msgsock</font><font color="#007700">);<br />} while (</font><font color="#0000BB">true</font><font color="#007700">);<br /><br /></font><font color="#0000BB">socket_close </font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN208076"
></A
><P
><B
>Exemplo 2. Exemplo de Socket: Simples cliente TCP/IP</B
></P
><P
>&#13;       Este exemplo mostra um simples, one-shot cliente HTTP. Isso simplesmente
       conecta à uma página, envia um cabeçalho de requisição, mostra a resposta, e sai.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />error_reporting </font><font color="#007700">(</font><font color="#0000BB">E_ALL</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"&lt;h2&gt;TCP/IP Connection&lt;/h2&gt;\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">/* Get the port for the WWW service. */<br /></font><font color="#0000BB">$service_port </font><font color="#007700">= </font><font color="#0000BB">getservbyname </font><font color="#007700">(</font><font color="#DD0000">'www'</font><font color="#007700">, </font><font color="#DD0000">'tcp'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Get the IP address for the target host. */<br /></font><font color="#0000BB">$address </font><font color="#007700">= </font><font color="#0000BB">gethostbyname </font><font color="#007700">(</font><font color="#DD0000">'www.example.com'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Create a TCP/IP socket. */<br /></font><font color="#0000BB">$socket </font><font color="#007700">= </font><font color="#0000BB">socket_create </font><font color="#007700">(</font><font color="#0000BB">AF_INET</font><font color="#007700">, </font><font color="#0000BB">SOCK_STREAM</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">);<br />if (</font><font color="#0000BB">$socket </font><font color="#007700">&lt; </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"socket_create() failed: reason: " </font><font color="#007700">. </font><font color="#0000BB">socket_strerror </font><font color="#007700">(</font><font color="#0000BB">$socket</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"OK.\n"</font><font color="#007700">;<br />}<br /><br />echo </font><font color="#DD0000">"Attempting to connect to '$address' on port '$service_port'..."</font><font color="#007700">;<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">socket_connect </font><font color="#007700">(</font><font color="#0000BB">$socket</font><font color="#007700">, </font><font color="#0000BB">$address</font><font color="#007700">, </font><font color="#0000BB">$service_port</font><font color="#007700">);<br />if (</font><font color="#0000BB">$result </font><font color="#007700">&lt; </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"socket_connect() failed.</font><font color="#007700">\n</font><font color="#DD0000">Reason: ($result) " </font><font color="#007700">. </font><font color="#0000BB">socket_strerror</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"OK.\n"</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">$in </font><font color="#007700">= </font><font color="#DD0000">"HEAD / HTTP/1.0\r\n\r\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$out </font><font color="#007700">= </font><font color="#DD0000">''</font><font color="#007700">;<br /><br />echo </font><font color="#DD0000">"Sending HTTP HEAD request..."</font><font color="#007700">;<br /></font><font color="#0000BB">socket_write </font><font color="#007700">(</font><font color="#0000BB">$socket</font><font color="#007700">, </font><font color="#0000BB">$in</font><font color="#007700">, </font><font color="#0000BB">strlen </font><font color="#007700">(</font><font color="#0000BB">$in</font><font color="#007700">));<br />echo </font><font color="#DD0000">"OK.\n"</font><font color="#007700">;<br /><br />echo </font><font color="#DD0000">"Reading response:\n\n"</font><font color="#007700">;<br />while (</font><font color="#0000BB">$out </font><font color="#007700">= </font><font color="#0000BB">socket_read </font><font color="#007700">(</font><font color="#0000BB">$socket</font><font color="#007700">, </font><font color="#0000BB">2048</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$out</font><font color="#007700">;<br />}<br /><br />echo </font><font color="#DD0000">"Closing socket..."</font><font color="#007700">;<br /></font><font color="#0000BB">socket_close </font><font color="#007700">(</font><font color="#0000BB">$socket</font><font color="#007700">);<br />echo </font><font color="#DD0000">"OK.\n\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Índice</B
></DT
><DT
><A
HREF="function.socket-accept.html"
>socket_accept</A
>&nbsp;--&nbsp;Aceita uma conexão com um socket</DT
><DT
><A
HREF="function.socket-bind.html"
>socket_bind</A
>&nbsp;--&nbsp;Passa um nome para o socket</DT
><DT
><A
HREF="function.socket-clear-error.html"
>socket_clear_error</A
>&nbsp;--&nbsp;Limpa o erro no socket ou o último código de erro</DT
><DT
><A
HREF="function.socket-close.html"
>socket_close</A
>&nbsp;--&nbsp;Fecha um socket resource</DT
><DT
><A
HREF="function.socket-connect.html"
>socket_connect</A
>&nbsp;--&nbsp;Inicializa uma conexão em um socket</DT
><DT
><A
HREF="function.socket-create-listen.html"
>socket_create_listen</A
>&nbsp;--&nbsp;Abre uma porta do socket para aceitar conexões</DT
><DT
><A
HREF="function.socket-create-pair.html"
>socket_create_pair</A
>&nbsp;--&nbsp;Cria um par de sockets irreconhecíveis e armazena-os no fds.</DT
><DT
><A
HREF="function.socket-create.html"
>socket_create</A
>&nbsp;--&nbsp;Cria um socket (endpoint para comunicação)</DT
><DT
><A
HREF="function.socket-get-option.html"
>socket_get_option</A
>&nbsp;--&nbsp;Pega opções de socket para o socket</DT
><DT
><A
HREF="function.socket-getpeername.html"
>socket_getpeername</A
>&nbsp;--&nbsp;
     Pergunta ao lado remoto do socket dado qual deve ser o resultado em host/port
     ou no endereço UNIX filesystem, dependendo deste tipo.
    </DT
><DT
><A
HREF="function.socket-getsockname.html"
>socket_getsockname</A
>&nbsp;--&nbsp;
     Pergunta o lado local dado do socket no qual deve retornar resultados no host/port ou no 
     endereço UNIX filesystem, dependendo do tipo.
    </DT
><DT
><A
HREF="function.socket-last-error.html"
>socket_last_error</A
>&nbsp;--&nbsp;Retorna o último erro no socket</DT
><DT
><A
HREF="function.socket-listen.html"
>socket_listen</A
>&nbsp;--&nbsp;Abre escuta para uma conexão no socket</DT
><DT
><A
HREF="function.socket-read.html"
>socket_read</A
>&nbsp;--&nbsp;Lê um comprimento máximo de bytes de um socket</DT
><DT
><A
HREF="function.socket-recv.html"
>socket_recv</A
>&nbsp;--&nbsp;Recebe dados de um socket conectado</DT
><DT
><A
HREF="function.socket-recvfrom.html"
>socket_recvfrom</A
>&nbsp;--&nbsp;Recebe dados de um socket, conectado ou não</DT
><DT
><A
HREF="function.socket-select.html"
>socket_select</A
>&nbsp;--&nbsp;
     Executa a chamada de sistema select() nos arrays dados de sockets 
     com um timeout especificado
    </DT
><DT
><A
HREF="function.socket-send.html"
>socket_send</A
>&nbsp;--&nbsp;Envia dados para um socket conectado</DT
><DT
><A
HREF="function.socket-sendto.html"
>socket_sendto</A
>&nbsp;--&nbsp;Envia uma mensagem para o socket, se estiver conectado ou não</DT
><DT
><A
HREF="function.socket-set-block.html"
>socket_set_block</A
>&nbsp;--&nbsp;
     Configura o modo bloqueio em um socket resource
    </DT
><DT
><A
HREF="function.socket-set-nonblock.html"
>socket_set_nonblock</A
>&nbsp;--&nbsp;Configura o modo nonblocking para o arquivo de descrição fd</DT
><DT
><A
HREF="function.socket-set-option.html"
>socket_set_option</A
>&nbsp;--&nbsp;Configura opções de socket para o socket</DT
><DT
><A
HREF="function.socket-shutdown.html"
>socket_shutdown</A
>&nbsp;--&nbsp;Fecha um socket para receber, enviar dados ou ambos. </DT
><DT
><A
HREF="function.socket-strerror.html"
>socket_strerror</A
>&nbsp;--&nbsp;Retorna uma string descrevendo o erro do socket</DT
><DT
><A
HREF="function.socket-write.html"
>socket_write</A
>&nbsp;--&nbsp;Escreve em um socket</DT
></DL
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="function.use-soap-error-handler.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Principal</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.socket-accept.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>use_soap_error_handler</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>socket_accept</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>