function.stream-socket-server.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 137 行
HTML
137 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Create an Internet or Unix domain server socket</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-socket-sendto.html">stream_socket_sendto</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-socket-shutdown.html">stream_socket_shutdown</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.stream-socket-server" class="refentry"> <div class="refnamediv"> <h1 class="refname">stream_socket_server</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">stream_socket_server</span> — <span class="dc-title">Create an Internet or Unix domain server socket</span></p> </div> <div class="refsect1 unknown"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><b><b>stream_socket_server</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$local_socket</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter reference">&$errno</tt></span> [, <span class="methodparam"><span class="type">string</span> <tt class="parameter reference">&$errstr</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$flags</tt></span> [, <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$context</tt></span> ]]]] )</div> <p class="para rdfs-comment"> Creates a stream or datagram socket on the specified <i><tt class="parameter">local_socket</tt></i>. The type of socket created is determined by the transport specified using standard URL formatting: <i>transport://target</i>. For Internet Domain sockets (AF_INET) such as TCP and UDP, the <i>target</i> portion of the <i><tt class="parameter">remote_socket</tt></i> parameter should consist of a hostname or IP address followed by a colon and a port number. For Unix domain sockets, the <i><tt class="parameter">target</tt></i> portion should point to the socket file on the filesystem. <i><tt class="parameter">flags</tt></i> is a bitmask field which may be set to any combination of socket creation flags. The default value of flags is <b><tt>STREAM_SERVER_BIND</tt></b> | <b><tt>STREAM_SERVER_LISTEN</tt></b>. </p> <blockquote><p><b class="note">Note</b>: For UDP sockets, you must use <b><tt>STREAM_SERVER_BIND</tt></b> as the <i><tt class="parameter">flags</tt></i> parameter. <br /> </p></blockquote> <p class="para"> This function only creates a socket, to begin accepting connections use <a href="function.stream-socket-accept.html" class="function">stream_socket_accept()</a>. </p> <p class="para"> If the call fails, it will return <b><tt>FALSE</tt></b> and if the optional <i><tt class="parameter">errno</tt></i> and <i><tt class="parameter">errstr</tt></i> arguments are present they will be set to indicate the actual system level error that occurred in the system-level <i>socket()</i>, <i>bind()</i>, and <i>listen()</i> calls. If the value returned in <i><tt class="parameter">errno</tt></i> is <i>0</i> and the function returned <b><tt>FALSE</tt></b>, it is an indication that the error occurred before the <i>bind()</i> call. This is most likely due to a problem initializing the socket. Note that the <i><tt class="parameter">errno</tt></i> and <i><tt class="parameter">errstr</tt></i> arguments will always be passed by reference. </p> <p class="para"> Depending on the environment, Unix domain sockets may not be available. A list of available transports can be retrieved using <a href="function.stream-get-transports.html" class="function">stream_get_transports()</a>. See <a href="transports.html" class="xref">List of Supported Socket Transports</a> for a list of bulitin transports. </p> <p class="para"> <div class="example"> <p><b>Example #1 Using TCP server sockets</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$socket </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_server</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://0.0.0.0:8000"</span><span style="color: #007700">, </span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$socket</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"$errstr ($errno)<br />\n"</span><span style="color: #007700">;<br />} else {<br /> while (</span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_accept</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">'The local time is ' </span><span style="color: #007700">. </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'n/j/Y g:i a'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /> }<br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> The example below shows how to act as a time server which can respond to time queries as shown in an example on <a href="function.stream-socket-client.html" class="function">stream_socket_client()</a>. <blockquote><p><b class="note">Note</b>: <span class="simpara"> Most systems require root access to create a server socket on a port below 1024. </span> </p></blockquote> <div class="example"> <p><b>Example #2 Using UDP server sockets</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$socket </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_server</span><span style="color: #007700">(</span><span style="color: #DD0000">"udp://127.0.0.1:1113"</span><span style="color: #007700">, </span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_SERVER_BIND</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$socket</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">"$errstr ($errno)"</span><span style="color: #007700">);<br />}<br /><br />do {<br /> </span><span style="color: #0000BB">$pkt </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$peer</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">"$peer\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">stream_socket_sendto</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"D M j H:i:s Y\r\n"</span><span style="color: #007700">), </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$peer</span><span style="color: #007700">);<br />} while (</span><span style="color: #0000BB">$pkt </span><span style="color: #007700">!== </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <blockquote><p><b class="note">Note</b>: <span class="simpara">When specifying a numerical IPv6 address(e.g. <i>fe80::1</i>), you must enclose the IP in squarebrackets鈥攆or example, <i>tcp://[fe80::1]:80</i>.</span></p></blockquote> <p class="para"> See also <a href="function.stream-socket-client.html" class="function">stream_socket_client()</a>, <a href="function.stream-set-blocking.html" class="function">stream_set_blocking()</a>, <a href="function.stream-set-timeout.html" class="function">stream_set_timeout()</a>, <a href="function.fgets.html" class="function">fgets()</a>, <a href="function.fgetss.html" class="function">fgetss()</a>, <a href="function.fwrite.html" class="function">fwrite()</a>, <a href="function.fclose.html" class="function">fclose()</a>, <a href="function.feof.html" class="function">feof()</a>, and the <a href="ref.curl.html" class="link">Curl extension</a>. </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-socket-sendto.html">stream_socket_sendto</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-socket-shutdown.html">stream_socket_shutdown</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?