<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Socket Functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="函数参考" 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" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.use-soap-error-handler.html" ACCESSKEY="P" >上一页</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" >下一页</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. Socket Functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN212409" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="sockets.intro" >简介</A ></H1 ><P > The socket extension implements a low-level interface to the socket communication functions based on the popular BSD sockets, providing the possibility to act as a socket server as well as a client. </P ><P > For a more generic client-side socket interface, see <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 >, and <A HREF="function.pfsockopen.html" ><B CLASS="function" >pfsockopen()</B ></A >. </P ><P > When using these functions, it is important to remember that while many of them have identical names to their C counterparts, they often have different declarations. Please be sure to read the descriptions to avoid confusion. </P ><P > Those unfamiliar with socket programming can find a lot of useful material in the appropriate Unix man pages, and there is a great deal of tutorial information on socket programming in C on the web, much of which can be applied, with slight modifications, to socket programming in PHP. The <A HREF="http://www.developerweb.net/sock-faq/" TARGET="_top" >Unix Socket FAQ</A > might be a good start. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 本扩展已被移动到 <A HREF="http://pecl.php.net" TARGET="_top" >PECL</A > 库中且自以下版本起不再被绑定到 PHP 中:5.3.0. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="sockets.requirements" >需求</A ></H1 ><P >要编译本扩展模块无需外部库文件。</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="sockets.installation" >安装</A ></H1 ><P > The socket functions described here are part of an extension to PHP which must be enabled at compile time by giving the <CODE CLASS="option" >--enable-sockets</CODE > option to <B CLASS="command" >configure</B >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B >IPv6 支持是 <TT CLASS="literal" >PHP 5.0.0</TT > 新加的。</P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="sockets.configuration" >运行时配置</A ></H1 ><P >本扩展模块在 <TT CLASS="filename" >php.ini</TT > 中未定义任何配置选项。</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="sockets.resources" >资源类型</A ></H1 ><P > <A HREF="function.socket-accept.html" ><B CLASS="function" >socket_accept()</B ></A >, <A HREF="function.socket-create-listen.html" ><B CLASS="function" >socket_create_listen()</B ></A > and <A HREF="function.socket-create.html" ><B CLASS="function" >socket_create()</B ></A > return socket recources. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="sockets.constants" >预定义常量</A ></H1 ><P >以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 PHP 中,或者在运行时被动态加载后才有效。</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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </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 > </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="sockets.errors" >Socket Errors</A ></H1 ><P > The socket extension was written to provide a usable interface to the powerful BSD sockets. Care has been taken that the functions work equally well on Win32 and Unix implementations. Almost all of the sockets functions may fail under certain conditions and therefore emit an <TT CLASS="constant" ><B >E_WARNING</B ></TT > message describing the error. Sometimes this doesn't happen to the desire of the developer. For example the function <A HREF="function.socket-read.html" ><B CLASS="function" >socket_read()</B ></A > may suddenly emit an <TT CLASS="constant" ><B >E_WARNING</B ></TT > message because the connection broke unexpectedly. It's common to suppress the warning with the <TT CLASS="literal" >@</TT >-operator and catch the error code within the application with the <A HREF="function.socket-last-error.html" ><B CLASS="function" >socket_last_error()</B ></A > function. You may call the <A HREF="function.socket-strerror.html" ><B CLASS="function" >socket_strerror()</B ></A > function with this error code to retrieve a string describing the error. See their description for more information. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > The <TT CLASS="constant" ><B >E_WARNING</B ></TT > messages generated by the socket extension are in English though the retrieved error message will appear depending on the current locale (<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" >范例</A ></H1 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN212658" ></A ><P ><B >例 1. Socket example: Simple TCP/IP server</B ></P ><P > This example shows a simple talkback server. Change the <CODE CLASS="varname" >address</CODE > and <CODE CLASS="varname" >port</CODE > variables to suit your setup and execute. You may then connect to the server with a command similar to: <B CLASS="command" >telnet 192.168.1.53 10000</B > (where the address and port match your setup). Anything you type will then be output on the server side, and echoed back to you. To disconnect, enter 'quit'. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> #!/usr/local/bin/php -q<br /><font color="#0000BB"><?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">SOL_TCP</font><font color="#007700">)) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> 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_last_error</font><font color="#007700">()) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br />if (</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">) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> 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">socket_last_error</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">socket_listen</font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">, </font><font color="#0000BB">5</font><font color="#007700">) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> 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">socket_last_error</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 />do {<br /> 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">)) === </font><font color="#0000BB">false</font><font color="#007700">) {<br /> 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">socket_last_error</font><font color="#007700">(</font><font color="#0000BB">$sock</font><font color="#007700">)) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /> break;<br /> }<br /> </font><font color="#FF8000">/* Send instructions. */<br /> </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 /> </font><font color="#DD0000">"To quit, type 'quit'. To shut down the server type 'shutdown'.\n"</font><font color="#007700">;<br /> </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 /> do {<br /> 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 /> 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">socket_last_error</font><font color="#007700">(</font><font color="#0000BB">$msgsock</font><font color="#007700">)) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /> break </font><font color="#0000BB">2</font><font color="#007700">;<br /> }<br /> 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 /> continue;<br /> }<br /> if (</font><font color="#0000BB">$buf </font><font color="#007700">== </font><font color="#DD0000">'quit'</font><font color="#007700">) {<br /> break;<br /> }<br /> if (</font><font color="#0000BB">$buf </font><font color="#007700">== </font><font color="#DD0000">'shutdown'</font><font color="#007700">) {<br /> </font><font color="#0000BB">socket_close</font><font color="#007700">(</font><font color="#0000BB">$msgsock</font><font color="#007700">);<br /> break </font><font color="#0000BB">2</font><font color="#007700">;<br /> }<br /> </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 /> </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 /> echo </font><font color="#DD0000">"$buf</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br /> } while (</font><font color="#0000BB">true</font><font color="#007700">);<br /> </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">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN212666" ></A ><P ><B >例 2. Socket example: Simple TCP/IP client</B ></P ><P > This example shows a simple, one-shot HTTP client. It simply connects to a page, submits a HEAD request, echoes the reply, and exits. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?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">"<h2>TCP/IP Connection</h2>\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">SOL_TCP</font><font color="#007700">);<br />if (</font><font color="#0000BB">$socket </font><font color="#007700">=== </font><font color="#0000BB">false</font><font color="#007700">) {<br /> 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_last_error</font><font color="#007700">()) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />} else {<br /> 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">=== </font><font color="#0000BB">false</font><font color="#007700">) {<br /> 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">socket_last_error</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 /> 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.1\r\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$in </font><font color="#007700">.= </font><font color="#DD0000">"Host: www.example.com\r\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$in </font><font color="#007700">.= </font><font color="#DD0000">"Connection: Close\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 /> 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">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >目录</B ></DT ><DT ><A HREF="function.socket-accept.html" >socket_accept</A > -- Accepts a connection on a socket</DT ><DT ><A HREF="function.socket-bind.html" >socket_bind</A > -- Binds a name to a socket</DT ><DT ><A HREF="function.socket-clear-error.html" >socket_clear_error</A > -- Clears the error on the socket or the last error code</DT ><DT ><A HREF="function.socket-close.html" >socket_close</A > -- Closes a socket resource</DT ><DT ><A HREF="function.socket-connect.html" >socket_connect</A > -- Initiates a connection on a socket</DT ><DT ><A HREF="function.socket-create-listen.html" >socket_create_listen</A > -- Opens a socket on port to accept connections </DT ><DT ><A HREF="function.socket-create-pair.html" >socket_create_pair</A > -- Creates a pair of indistinguishable sockets and stores them in an array</DT ><DT ><A HREF="function.socket-create.html" >socket_create</A > -- Create a socket (endpoint for communication)</DT ><DT ><A HREF="function.socket-get-option.html" >socket_get_option</A > -- Gets socket options for the socket </DT ><DT ><A HREF="function.socket-getpeername.html" >socket_getpeername</A > -- Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type </DT ><DT ><A HREF="function.socket-getsockname.html" >socket_getsockname</A > -- Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type </DT ><DT ><A HREF="function.socket-last-error.html" >socket_last_error</A > -- Returns the last error on the socket </DT ><DT ><A HREF="function.socket-listen.html" >socket_listen</A > -- Listens for a connection on a socket</DT ><DT ><A HREF="function.socket-read.html" >socket_read</A > -- Reads a maximum of length bytes from a socket</DT ><DT ><A HREF="function.socket-recv.html" >socket_recv</A > -- Receives data from a connected socket </DT ><DT ><A HREF="function.socket-recvfrom.html" >socket_recvfrom</A > -- Receives data from a socket, connected or not </DT ><DT ><A HREF="function.socket-select.html" >socket_select</A > -- Runs the select() system call on the given arrays of sockets with a specified timeout </DT ><DT ><A HREF="function.socket-send.html" >socket_send</A > -- Sends data to a connected socket </DT ><DT ><A HREF="function.socket-sendto.html" >socket_sendto</A > -- Sends a message to a socket, whether it is connected or not </DT ><DT ><A HREF="function.socket-set-block.html" >socket_set_block</A > -- Sets blocking mode on a socket resource </DT ><DT ><A HREF="function.socket-set-nonblock.html" >socket_set_nonblock</A > -- Sets nonblocking mode for file descriptor fd </DT ><DT ><A HREF="function.socket-set-option.html" >socket_set_option</A > -- Sets socket options for the socket</DT ><DT ><A HREF="function.socket-shutdown.html" >socket_shutdown</A > -- Shuts down a socket for receiving, sending, or both</DT ><DT ><A HREF="function.socket-strerror.html" >socket_strerror</A > -- Return a string describing a socket error</DT ><DT ><A HREF="function.socket-write.html" >socket_write</A > -- Write to a 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" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.socket-accept.html" ACCESSKEY="N" >下一页</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" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >socket_accept</TD ></TR ></TABLE ></DIV ></BODY ></HTML >