function.socket-create.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 303 行
HTML
303 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Create a socket (endpoint for communication)</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.socket-create-pair.html">socket_create_pair</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-get-option.html">socket_get_option</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.socket-create" class="refentry"> <div class="refnamediv"> <h1 class="refname">socket_create</h1> <p class="verinfo">(PHP 4 >= 4.0.7, PHP 5)</p><p class="refpurpose"><span class="refname">socket_create</span> — <span class="dc-title">Create a socket (endpoint for communication)</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><b><b>socket_create</b></b></span> ( <span class="methodparam"><span class="type">int</span> <tt class="parameter">$domain</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$type</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$protocol</tt></span> )</div> <p class="para rdfs-comment"> Creates and returns a socket resource, also referred to as an endpoint of communication. A typical network connection is made up of 2 sockets, one performing the role of the client, and another performing the role of the server. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">domain</tt></i></span> <dd> <p class="para"> The <i><tt class="parameter">domain</tt></i> parameter specifies the protocol family to be used by the socket. </p> <table border="5"> <caption><b>Available address/protocol families</b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Domain</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">AF_INET</td> <td colspan="1" rowspan="1" align="left"> IPv4 Internet based protocols. TCP and UDP are common protocols of this protocol family. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">AF_INET6</td> <td colspan="1" rowspan="1" align="left"> IPv6 Internet based protocols. TCP and UDP are common protocols of this protocol family. Support added in PHP 5.0.0. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">AF_UNIX</td> <td colspan="1" rowspan="1" align="left"> Local communication protocol family. High efficiency and low overhead make it a great form of IPC (Interprocess Communication). </td> </tr> </tbody> </colgroup> </table> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">type</tt></i></span> <dd> <p class="para"> The <i><tt class="parameter">type</tt></i> parameter selects the type of communication to be used by the socket. </p> <table border="5"> <caption><b>Available socket types</b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Type</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">SOCK_STREAM</td> <td colspan="1" rowspan="1" align="left"> Provides sequenced, reliable, full-duplex, connection-based byte streams. An out-of-band data transmission mechanism may be supported. The TCP protocol is based on this socket type. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">SOCK_DGRAM</td> <td colspan="1" rowspan="1" align="left"> Supports datagrams (connectionless, unreliable messages of a fixed maximum length). The UDP protocol is based on this socket type. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">SOCK_SEQPACKET</td> <td colspan="1" rowspan="1" align="left"> Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each read call. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">SOCK_RAW</td> <td colspan="1" rowspan="1" align="left"> Provides raw network protocol access. This special type of socket can be used to manually construct any type of protocol. A common use for this socket type is to perform ICMP requests (like ping, traceroute, etc). </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">SOCK_RDM</td> <td colspan="1" rowspan="1" align="left"> Provides a reliable datagram layer that does not guarantee ordering. This is most likely not implemented on your operating system. </td> </tr> </tbody> </colgroup> </table> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">protocol</tt></i></span> <dd> <p class="para"> The <i><tt class="parameter">protocol</tt></i> parameter sets the specific protocol within the specified <i><tt class="parameter">domain</tt></i> to be used when communicating on the returned socket. The proper value can be retrieved by name by using <a href="function.getprotobyname.html" class="function">getprotobyname()</a>. If the desired protocol is TCP, or UDP the corresponding constants <b><tt>SOL_TCP</tt></b>, and <b><tt>SOL_UDP</tt></b> can also be used. </p> <table border="5"> <caption><b>Common protocols</b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Name</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">icmp</td> <td colspan="1" rowspan="1" align="left"> The Internet Control Message Protocol is used primarily by gateways and hosts to report errors in datagram communication. The "ping" command (present in most modern operating systems) is an example application of ICMP. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">udp</td> <td colspan="1" rowspan="1" align="left"> The User Datagram Protocol is a connectionless, unreliable, protocol with fixed record lengths. Due to these aspects, UDP requires a minimum amount of protocol overhead. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">tcp</td> <td colspan="1" rowspan="1" align="left"> The Transmission Control Protocol is a reliable, connection based, stream oriented, full duplex protocol. TCP guarantees that all data packets will be received in the order in which they were sent. If any packet is somehow lost during communication, TCP will automatically retransmit the packet until the destination host acknowledges that packet. For reliability and performance reasons, the TCP implementation itself decides the appropriate octet boundaries of the underlying datagram communication layer. Therefore, TCP applications must allow for the possibility of partial record transmission. </td> </tr> </tbody> </colgroup> </table> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <b>socket_create()</b> returns a socket resource on success, or <b><tt>FALSE</tt></b> on error. The actual error code can be retrieved by calling <a href="function.socket-last-error.html" class="function">socket_last_error()</a>. This error code may be passed to <a href="function.socket-strerror.html" class="function">socket_strerror()</a> to get a textual explanation of the error. </p> </div> <div class="refsect1 errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> If an invalid <i><tt class="parameter">domain</tt></i> or <i><tt class="parameter">type</tt></i> is given, <b>socket_create()</b> defaults to <b><tt>AF_INET</tt></b> and <b><tt>SOCK_STREAM</tt></b> respectively and additionally emits an <b><tt>E_WARNING</tt></b> message. </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.socket-accept.html" class="function" rel="rdfs-seeAlso">socket_accept()</a></li> <li class="member"><a href="function.socket-bind.html" class="function" rel="rdfs-seeAlso">socket_bind()</a></li> <li class="member"><a href="function.socket-connect.html" class="function" rel="rdfs-seeAlso">socket_connect()</a></li> <li class="member"><a href="function.socket-listen.html" class="function" rel="rdfs-seeAlso">socket_listen()</a></li> <li class="member"><a href="function.socket-last-error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a></li> <li class="member"><a href="function.socket-strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.socket-create-pair.html">socket_create_pair</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-get-option.html">socket_get_option</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?