mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-17 21:16:57 +08:00
258 lines
14 KiB
HTML
258 lines
14 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>Open Internet or Unix domain socket connection</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.stream-socket-client" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">stream_socket_client</h1>
|
||
<p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">stream_socket_client</span> — <span class="dc-title">Open Internet or Unix domain socket connection</span></p>
|
||
|
||
</div>
|
||
|
||
<div class="refsect1 description" id="refsect1-function.stream-socket-client-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>stream_socket_client</strong></span>
|
||
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$remote_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">float</span> <code class="parameter">$timeout</code><span class="initializer"> = ini_get("default_socket_timeout")</span></span>
|
||
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = STREAM_CLIENT_CONNECT</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">
|
||
Initiates a stream or datagram connection to the destination specified
|
||
by <code class="parameter">remote_socket</code>. The type of socket created
|
||
is determined by the transport specified using standard URL formatting:
|
||
<em>transport://target</em>. For Internet Domain sockets
|
||
(AF_INET) 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 <code class="parameter">target</code> portion should point
|
||
to the socket file on the filesystem.
|
||
</p>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
The stream will by default be opened in blocking mode. You can
|
||
switch it to non-blocking mode by using
|
||
<span class="function"><a href="stream_set_blocking.html" class="function">stream_set_blocking()</a></span>.
|
||
</p>
|
||
</p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.stream-socket-client-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">remote_socket</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
Address to the socket to connect to.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">errno</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
Will be set to the system level error number if connection fails.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">errstr</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
Will be set to the system level error message if the connection fails.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">timeout</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
Number of seconds until the <em>connect()</em> system call
|
||
should timeout.
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<span class="simpara">
|
||
This parameter only applies when not making asynchronous
|
||
connection attempts.
|
||
</span>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
To set a timeout for reading/writing data over the socket, use the
|
||
<span class="function"><a href="stream_set_timeout.html" class="function">stream_set_timeout()</a></span>, as the
|
||
<code class="parameter">timeout</code> only applies while making connecting
|
||
the socket.
|
||
</p>
|
||
</p></blockquote>
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">flags</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
Bitmask field which may be set to any combination of connection flags.
|
||
Currently the select of connection flags is limited to
|
||
<strong><code>STREAM_CLIENT_CONNECT</code></strong> (default),
|
||
<strong><code>STREAM_CLIENT_ASYNC_CONNECT</code></strong> and
|
||
<strong><code>STREAM_CLIENT_PERSISTENT</code></strong>.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">context</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
A valid context resource created with <span class="function"><a href="stream_context_create.html" class="function">stream_context_create()</a></span>.
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.stream-socket-client-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
On success a stream resource is returned which may
|
||
be used together with the other file functions (such as
|
||
<span class="function"><a href="fgets.html" class="function">fgets()</a></span>, <span class="function"><a href="fgetss.html" class="function">fgetss()</a></span>,
|
||
<span class="function"><a href="fwrite.html" class="function">fwrite()</a></span>, <span class="function"><a href="fclose.html" class="function">fclose()</a></span>, and
|
||
<span class="function"><a href="feof.html" class="function">feof()</a></span>), <strong><code>FALSE</code></strong> on failure.
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 errors" id="refsect1-function.stream-socket-client-errors">
|
||
<h3 class="title">错误/异常</h3>
|
||
<p class="para">
|
||
On failure the <code class="parameter">errno</code> and
|
||
<code class="parameter">errstr</code> arguments will be populated with the actual
|
||
system level error that occurred in the system-level
|
||
<em>connect()</em> call. 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>connect()</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>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.stream-socket-client-examples">
|
||
<h3 class="title">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-4836">
|
||
<p><strong>Example #1 <span class="function"><strong>stream_socket_client()</strong></span> example</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_client</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://www.example.com:80"</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">30</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$fp</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 /> </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n"</span><span style="color: #007700">);<br /> while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">1024</span><span style="color: #007700">);<br /> }<br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
<div class="example" id="example-4837">
|
||
<p><strong>Example #2 Using UDP connection</strong></p>
|
||
<div class="example-contents"><p>
|
||
Retrieving the day and time from the UDP service "daytime" (port 13)
|
||
on localhost.
|
||
</p></div>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_client</span><span style="color: #007700">(</span><span style="color: #DD0000">"udp://127.0.0.1:13"</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">$fp</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"ERROR: </span><span style="color: #0000BB">$errno</span><span style="color: #DD0000"> - </span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br />} else {<br /> </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /> echo </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">26</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</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-client-notes">
|
||
<h3 class="title">注释</h3>
|
||
<div class="warning"><strong class="warning">Warning</strong>
|
||
<p class="simpara">
|
||
UDP sockets will sometimes appear to have opened without an error,
|
||
even if the remote host is unreachable. The error will only
|
||
become apparent when you read or write data to/from the socket.
|
||
The reason for this is because UDP is a "connectionless" protocol,
|
||
which means that the operating system does not try to establish
|
||
a link for the socket until it actually needs to send or receive data.
|
||
</p>
|
||
</div>
|
||
<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>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
Depending on the environment, the Unix domain or the optional
|
||
connect timeout 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 built in transports.
|
||
</p>
|
||
</p></blockquote>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 seealso" id="refsect1-function.stream-socket-client-seealso">
|
||
<h3 class="title">参见</h3>
|
||
<p class="para">
|
||
<ul class="simplelist">
|
||
<li class="member"><span class="function"><a href="stream_socket_server.html" class="function" rel="rdfs-seeAlso">stream_socket_server()</a> - Create an Internet or Unix domain server socket</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="stream_select.html" class="function" rel="rdfs-seeAlso">stream_select()</a> - Runs the equivalent of the select() system call on the given
|
||
arrays of streams with a timeout specified by tv_sec and tv_usec</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="xref">cURL 函数</a></li>
|
||
</ul>
|
||
</p>
|
||
</div>
|
||
|
||
|
||
</div></div></div></body></html> |