mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
205 lines
13 KiB
HTML
205 lines
13 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<title>Create an Internet or Unix domain server socket</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.stream-socket-server" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">stream_socket_server</h1>
|
||
<p class="verinfo">(PHP 5, PHP 7)</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 description" id="refsect1-function.stream-socket-server-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>stream_socket_server</strong></span>
|
||
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$local_socket</code></span>
|
||
[, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$errno</code></span>
|
||
[, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$errstr</code></span>
|
||
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN</span></span>
|
||
[, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span>
|
||
]]]] ) : <span class="type">resource</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
Creates a stream or datagram socket on the specified
|
||
<code class="parameter">local_socket</code>.
|
||
</p>
|
||
<p class="para">
|
||
This function only creates a socket, to begin accepting connections
|
||
use <span class="function"><a href="stream_socket_accept.html" class="function">stream_socket_accept()</a></span>.
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.stream-socket-server-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">local_socket</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
The type of socket created is determined by the transport specified
|
||
using standard URL formatting: <em>transport://target</em>.
|
||
</p>
|
||
<p class="para">
|
||
For Internet Domain sockets (<strong><code>AF_INET</code></strong>) such as TCP and UDP, the
|
||
<em>target</em> portion of the
|
||
<code class="parameter">remote_socket</code> parameter should consist of a
|
||
hostname or IP address followed by a colon and a port number. For
|
||
Unix domain sockets, the <em>target</em> portion should
|
||
point to the socket file on the filesystem.
|
||
</p>
|
||
<p class="para">
|
||
Depending on the environment, Unix domain sockets may not be available.
|
||
A list of available transports can be retrieved using
|
||
<span class="function"><a href="stream_get_transports.html" class="function">stream_get_transports()</a></span>. See
|
||
<a href="transports.html" class="xref">所支持的套接字传输器(Socket Transports)列表</a> for a list of bulitin transports.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">errno</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
If the optional <code class="parameter">errno</code> and <code class="parameter">errstr</code>
|
||
arguments are present they will be set to indicate the actual system
|
||
level error that occurred in the system-level <em>socket()</em>,
|
||
<em>bind()</em>, and <em>listen()</em> calls. If
|
||
the value returned in <code class="parameter">errno</code> is
|
||
<em>0</em> and the function returned <strong><code>FALSE</code></strong>, it is an
|
||
indication that the error occurred before the <em>bind()</em>
|
||
call. This is most likely due to a problem initializing the socket.
|
||
Note that the <code class="parameter">errno</code> and
|
||
<code class="parameter">errstr</code> arguments will always be passed by reference.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">errstr</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
See <code class="parameter">errno</code> description.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">flags</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
A bitmask field which may be set to any combination of socket creation
|
||
flags.
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
For UDP sockets, you must use <strong><code>STREAM_SERVER_BIND</code></strong> as
|
||
the <code class="parameter">flags</code> parameter.
|
||
</p>
|
||
</p></blockquote>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">context</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.stream-socket-server-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
Returns the created stream, or <strong><code>FALSE</code></strong> on error.
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.stream-socket-server-examples">
|
||
<h3 class="title">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-4842">
|
||
<p><strong>Example #1 Using TCP server sockets</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><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">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)<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>
|
||
</pre></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 <span class="function"><a href="stream_socket_client.html" class="function">stream_socket_client()</a></span>.
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<span class="simpara">
|
||
Most systems require root access to create a server socket on a port
|
||
below 1024.
|
||
</span>
|
||
</p></blockquote>
|
||
<div class="example" id="stream-notification-callback.example.basic">
|
||
<p><strong>Example #2 Using UDP server sockets</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><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">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)"</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">"</span><span style="color: #0000BB">$peer</span><span style="color: #DD0000">\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>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 notes" id="refsect1-function.stream-socket-server-notes">
|
||
<h3 class="title">注释</h3>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">当指定数值型的 IPv6
|
||
地址(例如 <em>fe80::1</em>)时必须用方括号将 IP 围起来——例如,
|
||
<em>tcp://[fe80::1]:80</em>。</span></p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 seealso" id="refsect1-function.stream-socket-server-seealso">
|
||
<h3 class="title">参见</h3>
|
||
<ul class="simplelist">
|
||
<li class="member"><span class="function"><a href="stream_socket_client.html" class="function" rel="rdfs-seeAlso">stream_socket_client()</a> - Open Internet or Unix domain socket connection</span></li>
|
||
<li class="member"><span class="function"><a href="stream_set_blocking.html" class="function" rel="rdfs-seeAlso">stream_set_blocking()</a> - 为资源流设置阻塞或者阻塞模式</span></li>
|
||
<li class="member"><span class="function"><a href="stream_set_timeout.html" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
|
||
<li class="member"><span class="function"><a href="fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
|
||
<li class="member"><span class="function"><a href="fgetss.html" class="function" rel="rdfs-seeAlso">fgetss()</a> - 从文件指针中读取一行并过滤掉 HTML 标记</span></li>
|
||
<li class="member"><span class="function"><a href="fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a> - 写入文件(可安全用于二进制文件)</span></li>
|
||
<li class="member"><span class="function"><a href="fclose.html" class="function" rel="rdfs-seeAlso">fclose()</a> - 关闭一个已打开的文件指针</span></li>
|
||
<li class="member"><span class="function"><a href="feof.html" class="function" rel="rdfs-seeAlso">feof()</a> - 测试文件指针是否到了文件结束的位置</span></li>
|
||
<li class="member"><a href="ref.curl.html" class="link">Curl extension</a></li>
|
||
</ul>
|
||
</div>
|
||
|
||
</div></div></div></body></html> |