Ref/sockets-Phpdoc专题
socket_accept
Accepts a connection on a socket
说明
resource <span
class="methodname">socket_accept ( <span
class="methodparam">resource $socket
)
After the socket socket has been created using <span
class="function">socket_create, bound to a name with <span
class="function">socket_bind, and told to listen for connections
with socket_listen, this function will
accept incoming connections on that socket. Once a successful connection
is made, a new socket resource is returned, which may be used for
communication. If there are multiple connections queued on the socket,
the first will be used. If there are no pending connections, <span
class="function">socket_accept will block until a connection
becomes present. If socket has been made non-blocking using <span
class="function">socket_set_blocking or <span
class="function">socket_set_nonblock, false will be
returned.
The socket resource returned by <span
class="function">socket_accept may not be used to accept new
connections. The original listening socket socket, however, remains
open and may be reused.
参数
socket
A valid socket resource created with <span
class="function">socket_create.
返回值
Returns a new socket resource on success, or false on error. The
actual error code can be retrieved by calling <span
class="function">socket_last_error. This error code may be
passed to socket_strerror to get a
textual explanation of the error.
参见
- socket_connect
- socket_listen
- socket_create
- socket_bind
- socket_strerror
socket_addrinfo_bind
Create and bind to a socket from a given addrinfo
说明
resource <span
class="methodname">socket_addrinfo_bind ( <span
class="methodparam">resource $addr )
Create a Socket resource, and bind it to the provided AddrInfo resource. The return value of this function may be used with <span class="function">socket_listen.
参数
addr
Resource created from <span
class="function">socket_addrinfo_lookup.
返回值
Returns a Socket resource on success or null on failure.
参见
- socket_addrinfo_connect
- socket_addrinfo_explain
- socket_addrinfo_lookup
- socket_listen
socket_addrinfo_connect
Create and connect to a socket from a given addrinfo
说明
resource <span
class="methodname">socket_addrinfo_connect ( <span
class="methodparam">resource $addr )
Create a Socket resource, and connect it to the provided AddrInfo resource. The return value of this function may be used with the rest of the socket functions.
参数
addr
Resource created from <span
class="function">socket_addrinfo_lookup
返回值
Returns a Socket resource on success or null on failure.
参见
- socket_addrinfo_bind
- socket_addrinfo_explain
- socket_addrinfo_lookup
socket_addrinfo_explain
Get information about addrinfo
说明
array <span
class="methodname">socket_addrinfo_explain ( <span
class="methodparam">resource $addr )
socket_addrinfo_explain exposed the underlying addrinfo structure.
参数
addr
Resource created from <span
class="function">socket_addrinfo_lookup
返回值
Returns an array containing the fields in the addrinfo structure.
参见
- socket_addrinfo_bind
- socket_addrinfo_connect
- socket_addrinfo_lookup
socket_addrinfo_lookup
Get array with contents of getaddrinfo about the given hostname
说明
array <span
class="methodname">socket_addrinfo_lookup ( <span
class="methodparam">string $host [,
string
$service [, <span
class="type">array $hints ]] )
Lookup different ways we can connect to host. The returned array
contains a set of resources that we can bind to using <span
class="function">socket_addrinfo_bind.
参数
host
Hostname to search.
service
The service to connect to. If service is a name, it is translated to the
corresponding port number.
hints
Hints provide criteria for selecting addresses returned. You may specify
the hints as defined by getadrinfo.
返回值
Returns an array of AddrInfo resource handles that can be used with the other socket_addrinfo functions.
参见
- socket_addrinfo_bind
- socket_addrinfo_connect
- socket_addrinfo_explain
socket_bind
给套接字绑定名字
说明
bool <span
class="methodname">socket_bind ( <span
class="type">resource $socket , <span
class="methodparam">string $address
[, int $port<span
class="initializer"> = 0 ] )
绑定 address 到 socket。 该操作必须是在使用 <span
class="function">socket_connect 或者 <span
class="function">socket_listen 建立一个连接之前。
参数
socket
用 socket_create
创建的一个有效的套接字资源。
address
如果套接字是 AF_INET 族,那么 address 必须是一个四点分法的 IP
地址(例如 127.0.0.1 )。
如果套接字是 AF_UNIX 族,那么 address 是 Unix 套接字一部分(例如
/tmp/my.sock )。
port (可选)
参数 port 仅仅用于 AF_INET
套接字连接的时候,并且指定连接中需要监听的端口号。
返回值
成功时返回 true, 或者在失败时返回 false。
错误代码会传入 socket_last_error ,如果将此参数传入 socket_strerror 则可以得到错误的文字说明。
范例
示例 #1 使用 socket_bind 来设置套接字资源地址
<?php
// Create a new socket
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// An example list of IP addresses owned by the computer
$sourceips['kevin'] = '127.0.0.1';
$sourceips['madcoder'] = '127.0.0.2';
// Bind the source address
socket_bind($sock, $sourceips['madcoder']);
// Connect to destination address
socket_connect($sock, '127.0.0.1', 80);
// Write
$request = 'GET / HTTP/1.1' . "\r\n" .
'Host: example.com' . "\r\n\r\n";
socket_write($sock, $request);
// Close
socket_close($sock);
?>
注释
Note:
该函数必须在 socket_connect 之前使用。
Note:
Windows 9x/ME 兼容性注意点: 如果尝试绑定套接字资源到一个错误的地址,而这个地址不是本机的地址,那么 socket_last_error 可能会返回一个无效的错误代码。
参见
- socket_connect
- socket_listen
- socket_create
- socket_last_error
- socket_strerror
socket_clear_error
清除套接字或者最后的错误代码上的错误
说明
void <span
class="methodname">socket_clear_error ([ <span
class="methodparam">resource $socket
] )
这个函数清除给定的套接字上的错误代码或是最后一个全局的套接字如果套接字没有指定的话
这个函数允许明确的重置错误代码值 不论是一个套接字或者最后全局错误代码的扩展, 这对在检测应用的一部分是否有错误发生是十分有用的
参数
socket
用socket_create创建的有效的套接字资源。
返回值
没有返回值。
参见
- socket_last_error
- socket_strerror
socket_close
关闭套接字资源
说明
void <span
class="methodname">socket_close ( <span
class="methodparam">resource $socket
)
socket_close 会关闭掉给定的 socket
资源。 这个函数只针对套接字资源有效,不能用在其他类型的资源类型上。
参数
socket
由 socket_create 或者是 <span
class="function">socket_accept 创建的有效的套接字资源。
返回值
没有返回值。
参见
- socket_bind
- socket_listen
- socket_create
- socket_strerror
socket_cmsg_space
Calculate message buffer size
说明
int <span
class="methodname">socket_cmsg_space ( <span
class="methodparam">int $level ,
int $type
[, int $n<span
class="initializer"> = 0 ] )
Calculates the size of the buffer that should be allocated for receiving the ancillary data.
Warning
本函数还未编写文档,仅有参数列表。
参数
level
type
返回值
参见
- socket_recvmsg
- socket_sendmsg
socket_connect
开启一个套接字连接
说明
bool <span
class="methodname">socket_connect ( <span
class="methodparam">resource $socket
, string
$address [, <span
class="type">int $port =
0 ] )
用 socket_create
创建的有效的套接字资源来连接到 address 。
参数
socket
address
如果参数 socket 是 AF_INET , 那么参数 address
则可以是一个点分四组表示法(例如 127.0.0.1 ) 的 IPv4 地址; 如果支持
IPv6 并且 socket 是 AF_INET6,那么 address 也可以是有效的 IPv6
地址(例如 ::1);如果套接字类型为 AF_UNIX ,那么 address
也可以是一个Unix 套接字。
port
参数 port 仅仅用于 AF_INET 和 AF_INET6
套接字连接的时候,并且是在此情况下是需要强制说明连接对应的远程服务器上的端口号。
返回值
成功时返回 true, 或者在失败时返回 false。 错误代码会传入
socket_last_error ,如果将此参数传入
socket_strerror
则可以得到错误的文字说明。
Note:
If the socket is non-blocking then this function returns
falsewith an error Operation now in progress.
参见
- socket_bind
- socket_listen
- socket_create
- socket_last_error
- socket_strerror
socket_create_listen
Opens a socket on port to accept connections
说明
resource <span
class="methodname">socket_create_listen ( <span
class="methodparam">int $port [,
int $backlog<span
class="initializer"> = 128 ] )
socket_create_listen creates a new
socket resource of type AF_INET listening on all local
interfaces on the given port waiting for new connections.
This function is meant to ease the task of creating a new socket which only listens to accept new connections.
参数
port
The port on which to listen on all interfaces.
backlog
The backlog parameter defines the maximum length the queue of pending
connections may grow to. SOMAXCONN may be passed as backlog
parameter, see socket_listen for more
information.
返回值
socket_create_listen returns a new
socket resource on success or false on error. The error code can
be retrieved with socket_last_error.
This code may be passed to <span
class="function">socket_strerror to get a textual explanation of
the error.
注释
Note:
If you want to create a socket which only listens on a certain interface you need to use <span class="function">socket_create, <span class="function">socket_bind and <span class="function">socket_listen.
参见
- socket_create
- socket_create_pair
- socket_bind
- socket_listen
- socket_last_error
- socket_strerror
socket_create_pair
Creates a pair of indistinguishable sockets and stores them in an array
说明
bool <span
class="methodname">socket_create_pair ( <span
class="methodparam">int $domain ,
int $type ,
int
$protocol , <span
class="type">array &$fd )
socket_create_pair creates two connected
and indistinguishable sockets, and stores them in fd. This function is
commonly used in IPC (InterProcess Communication).
参数
domain
The domain parameter specifies the protocol family to be used by the
socket. See socket_create for the full
list.
type
The type parameter selects the type of communication to be used by the
socket. See socket_create for the full
list.
protocol
The protocol parameter sets the specific protocol within the specified
domain to be used when communicating on the returned socket. The
proper value can be retrieved by name by using <span
class="function">getprotobyname. If the desired protocol is TCP,
or UDP the corresponding constants SOL_TCP, and SOL_UDP can
also be used.
See socket_create for the full list of supported protocols.
fd
Reference to an array in which the two socket resources will be
inserted.
返回值
成功时返回 true, 或者在失败时返回 false。
范例
示例 #1 socket_create_pair example
<?php
$sockets = array();
/* On Windows we need to use AF_INET */
$domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX);
/* Setup socket pair */
if (socket_create_pair($domain, SOCK_STREAM, 0, $sockets) === false) {
echo "socket_create_pair failed. Reason: ".socket_strerror(socket_last_error());
}
/* Send and Receive Data */
if (socket_write($sockets[0], "ABCdef123\n", strlen("ABCdef123\n")) === false) {
echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($sockets[0]));
}
if (($data = socket_read($sockets[1], strlen("ABCdef123\n"), PHP_BINARY_READ)) === false) {
echo "socket_read() failed. Reason: ".socket_strerror(socket_last_error($sockets[1]));
}
var_dump($data);
/* Close sockets */
socket_close($sockets[0]);
socket_close($sockets[1]);
?>
示例 #2 socket_create_pair IPC example
<?php
$ary = array();
$strone = 'Message From Parent.';
$strtwo = 'Message From Child.';
if (socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $ary) === false) {
echo "socket_create_pair() failed. Reason: ".socket_strerror(socket_last_error());
}
$pid = pcntl_fork();
if ($pid == -1) {
echo 'Could not fork Process.';
} elseif ($pid) {
/*parent*/
socket_close($ary[0]);
if (socket_write($ary[1], $strone, strlen($strone)) === false) {
echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[1]));
}
if (socket_read($ary[1], strlen($strtwo), PHP_BINARY_READ) == $strtwo) {
echo "Received $strtwo\n";
}
socket_close($ary[1]);
} else {
/*child*/
socket_close($ary[1]);
if (socket_write($ary[0], $strtwo, strlen($strtwo)) === false) {
echo "socket_write() failed. Reason: ".socket_strerror(socket_last_error($ary[0]));
}
if (socket_read($ary[0], strlen($strone), PHP_BINARY_READ) == $strone) {
echo "Received $strone\n";
}
socket_close($ary[0]);
}
?>
参见
- socket_create
- socket_create_listen
- socket_bind
- socket_listen
- socket_last_error
- socket_strerror
socket_create
创建一个套接字(通讯节点)
说明
resource <span
class="methodname">socket_create ( <span
class="methodparam">int $domain ,
int $type ,
int
$protocol )
创建并返回一个套接字,也称作一个通讯节点。一个典型的网络连接由 2 个套接字构成,一个运行在客户端,另一个运行在服务器端。
参数
domain
domain 参数指定哪个协议用在当前套接字上。
| Domain | 描述 |
|---|---|
AF_INET |
IPv4 网络协议。TCP 和 UDP 都可使用此协议。 |
AF_INET6 |
IPv6 网络协议。TCP 和 UDP 都可使用此协议。 |
AF_UNIX |
本地通讯协议。具有高性能和低成本的 IPC(进程间通讯)。 |
type
type 参数用于选择套接字使用的类型。
| 类型 | 描述 |
|---|---|
SOCK_STREAM |
提供一个顺序化的、可靠的、全双工的、基于连接的字节流。支持数据传送流量控制机制。TCP 协议即基于这种流式套接字。 |
SOCK_DGRAM |
提供数据报文的支持。(无连接,不可靠、固定最大长度).UDP协议即基于这种数据报文套接字。 |
SOCK_SEQPACKET |
提供一个顺序化的、可靠的、全双工的、面向连接的、固定最大长度的数据通信;数据端通过接收每一个数据段来读取整个数据包。 |
SOCK_RAW |
提供读取原始的网络协议。这种特殊的套接字可用于手工构建任意类型的协议。一般使用这个套接字来实现 ICMP 请求(例如 ping)。 |
SOCK_RDM |
提供一个可靠的数据层,但不保证到达顺序。一般的操作系统都未实现此功能。 |
protocol
protocol 参数,是设置指定 domain 套接字下的具体协议。这个值可以使用
getprotobyname
函数进行读取。如果所需的协议是 TCP 或 UDP,可以直接使用常量
SOL_TCP 和 SOL_UDP 。
| 名称 | 描述 |
|---|---|
| icmp | Internet Control Message Protocol 主要用于网关和主机报告错误的数据通信。例如“ping”命令(在目前大部分的操作系统中)就是使用 ICMP 协议实现的。 |
| udp | User Datagram Protocol 是一个无连接的、不可靠的、具有固定最大长度的报文协议。由于这些特性,UDP 协议拥有最小的协议开销。 |
| tcp | Transmission Control Protocol 是一个可靠的、基于连接的、面向数据流的全双工协议。TCP 能够保障所有的数据包是按照其发送顺序而接收的。如果任意数据包在通讯时丢失,TCP 将自动重发数据包直到目标主机应答已接收。因为可靠性和性能的原因,TCP 在数据传输层使用 8bit 字节边界。因此,TCP 应用程序必须允许传送部分报文的可能。 |
返回值
socket_create
正确时返回一个套接字,失败时返回 false。要读取错误代码,可以调用
socket_last_error。这个错误代码可以通过
socket_strerror 读取文字的错误说明。
更新日志
| 版本 | 说明 |
|---|---|
| 5.0.0 | 增加 AF_INET6 支持。 |
错误/异常
如果使用一个无效的 domain 或 type,<span
class="function">socket_create 会使用 AF_INET 和
SOCK_STREAM 替代无效参数,同时会发出 E_WARNING 警告信息。
参见
- socket_accept
- socket_bind
- socket_connect
- socket_listen
- socket_last_error
- socket_strerror
socket_export_stream
Export a socket extension resource into a stream that encapsulates a socket
说明
resource<span
class="type">false <span
class="methodname">socket_export_stream ( <span
class="methodparam">resource $socket
)
Warning
本函数还未编写文档,仅有参数列表。
参数
socket
返回值
Return resource 或者在失败时返回 false.
socket_get_option
Gets socket options for the socket
说明
mixed <span
class="methodname">socket_get_option ( <span
class="methodparam">resource $socket
, int
$level , <span
class="type">int $optname )
The socket_get_option function retrieves
the value for the option specified by the optname parameter for the
specified socket.
参数
socket
A valid socket resource created with <span
class="function">socket_create or <span
class="function">socket_accept.
level
The level parameter specifies the protocol level at which the option
resides. For example, to retrieve options at the socket level, a level
parameter of SOL_SOCKET would be used. Other levels, such as
TCP, can be used by specifying the protocol number of that level.
Protocol numbers can be found by using the <span
class="function">getprotobyname function.
optname
| Option | Description | Type |
|---|---|---|
SO_DEBUG |
Reports whether debugging information is being recorded. | int |
SO_BROADCAST |
Reports whether transmission of broadcast messages is supported. | int |
SO_REUSEADDR |
Reports whether local addresses can be reused. | int |
SO_REUSEPORT |
Reports whether local ports can be reused. | int |
SO_KEEPALIVE |
Reports whether connections are kept active with periodic transmission of messages. If the connected socket fails to respond to these messages, the connection is broken and processes writing to that socket are notified with a SIGPIPE signal. | int |
SO_LINGER |
Reports whether the If l_onoff is non-zero and l_linger is zero, all the unsent data will be discarded and RST (reset) is sent to the peer in the case of a connection-oriented socket. On the other hand, if l_onoff is non-zero and l_linger is non-zero, socket_close will block until all the data is sent or the time specified in l_linger elapses. If the socket is non-blocking, socket_close will fail and return an error. |
array. The array will contain two keys: l_onoff and l_linger. |
SO_OOBINLINE |
Reports whether the socket leaves out-of-band data inline. |
int |
SO_SNDBUF |
Reports the size of the send buffer. | int |
SO_RCVBUF |
Reports the size of the receive buffer. | int |
SO_ERROR |
Reports information about error status and clears it. | int (cannot be set by socket_set_option) |
SO_TYPE |
Reports the socket type (e.g. SOCK_STREAM). |
int (cannot be set by socket_set_option) |
SO_DONTROUTE |
Reports whether outgoing messages bypass the standard routing facilities. | int |
SO_RCVLOWAT |
Reports the minimum number of bytes to process for socket input operations. |
int |
SO_RCVTIMEO |
Reports the timeout value for input operations. | array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value. |
SO_SNDTIMEO |
Reports the timeout value specifying the amount of time that an output function blocks because flow control prevents data from being sent. | array. The array will contain two keys: sec which is the seconds part on the timeout value and usec which is the microsecond part of the timeout value. |
SO_SNDLOWAT |
Reports the minimum number of bytes to process for socket output operations. |
int |
TCP_NODELAY |
Reports whether the Nagle TCP algorithm is disabled. | int |
MCAST_JOIN_GROUP |
Joins a multicast group. (added in PHP 5.4) | array with keys "group", specifying a string with an IPv4 or IPv6 multicast address and "interface", specifying either an interface number (type int) or a string with the interface name, like "eth0". 0 can be specified to indicate the interface should be selected using routing rules. (can only be used in socket_set_option) |
MCAST_LEAVE_GROUP |
Leaves a multicast group. (added in PHP 5.4) | array. See MCAST_JOIN_GROUP for more information. (can only be used in socket_set_option) |
MCAST_BLOCK_SOURCE |
Blocks packets arriving from a specific source to a specific multicast group, which must have been previously joined. (added in PHP 5.4) | array with the same keys as MCAST_JOIN_GROUP, plus one extra key, source, which maps to a string specifying an IPv4 or IPv6 address of the source to be blocked. (can only be used in socket_set_option) |
MCAST_UNBLOCK_SOURCE |
Unblocks (start receiving again) packets arriving from a specific source address to a specific multicast group, which must have been previously joined. (added in PHP 5.4) | array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option) |
MCAST_JOIN_SOURCE_GROUP |
Receive packets destined to a specific multicast group whose source address matches a specific value. (added in PHP 5.4) | array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option) |
MCAST_LEAVE_SOURCE_GROUP |
Stop receiving packets destined to a specific multicast group whose source address matches a specific value. (added in PHP 5.4) | array with the same format as MCAST_BLOCK_SOURCE. (can only be used in socket_set_option) |
IP_MULTICAST_IF |
The outgoing interface for IPv4 multicast packets. (added in PHP 5.4) | Either int specifying the interface number or a string with an interface name, like eth0. The value 0 can be used to indicate the routing table is to used in the interface selection. The function socket_get_option returns an interface index. Note that, unlike the C API, this option does NOT take an IP address. This eliminates the interface difference between IP_MULTICAST_IF and IPV6_MULTICAST_IF. |
IPV6_MULTICAST_IF |
The outgoing interface for IPv6 multicast packets. (added in PHP 5.4) | The same as IP_MULTICAST_IF. |
IP_MULTICAST_LOOP |
The multicast loopback policy for IPv4 packets, which determines whether multicast packets sent by this socket also reach receivers in the same host that have joined the same multicast group on the outgoing interface used by this socket. This is the case by default. (added in PHP 5.4) | int (either 0 or 1). For socket_set_option any value will be accepted and will be converted to a boolean following the usual PHP rules. |
IPV6_MULTICAST_LOOP |
Analogous to IP_MULTICAST_LOOP, but for IPv6. (added in PHP 5.4) |
int. See IP_MULTICAST_LOOP. |
IP_MULTICAST_TTL |
The time-to-live of outgoing IPv4 multicast packets. This should be a value between 0 (don't leave the interface) and 255. The default value is 1 (only the local network is reached). (added in PHP 5.4) | int between 0 and 255. |
IPV6_MULTICAST_HOPS |
Analogous to IP_MULTICAST_TTL, but for IPv6 packets. The value -1 is also accepted, meaning the route default should be used. (added in PHP 5.4) |
int between -1 and 255. |
返回值
Returns the value of the given option, or false on errors.
范例
示例 #1 socket_get_option example
<?php
$socket = socket_create_listen(1223);
$linger = array('l_linger' => 1, 'l_onoff' => 1);
socket_set_option($socket, SOL_SOCKET, SO_LINGER, $linger);
var_dump(socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR));
?>
参见
- socket_create_listen
- socket_set_option
socket_getopt
别名 socket_get_option
说明
此函数是该函数的别名: <span class="function">socket_get_option.
socket_getpeername
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
说明
bool <span
class="methodname">socket_getpeername ( <span
class="methodparam">resource $socket
, string
&$address [, <span
class="type">int &$port ] )
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.
参数
socket
A valid socket resource created with <span
class="function">socket_create or <span
class="function">socket_accept.
address
If the given socket is of type AF_INET or AF_INET6, <span
class="function">socket_getpeername will return the peers
(remote) IP address in appropriate notation (e.g. 127.0.0.1 or
fe80::1) in the address parameter and, if the optional port
parameter is present, also the associated port.
If the given socket is of type AF_UNIX, <span
class="function">socket_getpeername will return the Unix
filesystem path (e.g. /var/run/daemon.sock) in the address
parameter.
port
If given, this will hold the port associated to address.
返回值
成功时返回 true, 或者在失败时返回 false。 <span
class="function">socket_getpeername may also return false
if the socket type is not any of AF_INET, AF_INET6, or
AF_UNIX, in which case the last socket error code is not
updated.
注释
Note:
socket_getpeername should not be used with
AF_UNIXsockets created with <span class="function">socket_accept. Only sockets created with socket_connect or a primary server socket following a call to socket_bind will return meaningful values.
Note:
For having socket_getpeername to return a meaningful value, the socket it is applied upon must of course be one for which the concept of "peer" makes sense.
参见
- socket_getsockname
- socket_last_error
- socket_strerror
socket_getsockname
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
说明
bool <span
class="methodname">socket_getsockname ( <span
class="methodparam">resource $socket
, string
&$addr [, <span
class="type">int &$port ] )
Note: <span class="function">socket_getsockname should not be used with
AF_UNIXsockets created with <span class="function">socket_connect. Only sockets created with socket_accept or a primary server socket following a call to socket_bind will return meaningful values.
参数
socket
A valid socket resource created with <span
class="function">socket_create or <span
class="function">socket_accept.
addr
If the given socket is of type AF_INET or AF_INET6, <span
class="function">socket_getsockname will return the local IP
address in appropriate notation (e.g. 127.0.0.1 or fe80::1) in the
address parameter and, if the optional port parameter is present,
also the associated port.
If the given socket is of type AF_UNIX, <span
class="function">socket_getsockname will return the Unix
filesystem path (e.g. /var/run/daemon.sock) in the address
parameter.
port
If provided, this will hold the associated port.
返回值
成功时返回 true, 或者在失败时返回 false。 <span
class="function">socket_getsockname may also return false
if the socket type is not any of AF_INET, AF_INET6, or
AF_UNIX, in which case the last socket error code is not
updated.
参见
- socket_getpeername
- socket_last_error
- socket_strerror
socket_import_stream
Import a stream
说明
resource <span
class="methodname">socket_import_stream ( <span
class="methodparam">resource $stream
)
Imports a stream that encapsulates a socket into a socket extension resource.
参数
stream
The stream resource to import.
返回值
Returns false or null on failure.
范例
示例 #1 socket_import_stream example
<?php
$stream = stream_socket_server("udp://0.0.0.0:58380", $errno, $errstr, STREAM_SERVER_BIND);
$sock = socket_import_stream($stream);
?>
参见
- stream_socket_server
socket_last_error
Returns the last error on the socket
说明
int <span
class="methodname">socket_last_error ([ <span
class="methodparam">resource $socket
] )
If a socket resource is passed to this function, the last error which occurred on this particular socket is returned. If the socket resource is omitted, the error code of the last failed socket function is returned. The latter is particularly helpful for functions like <span class="function">socket_create which don't return a socket on failure and socket_select which can fail for reasons not directly tied to a particular socket. The error code is suitable to be fed to socket_strerror which returns a string describing the given error code.
If no error had occurred, or the error had been cleared with <span class="function">socket_clear_error, the function returns 0.
参数
socket
A valid socket resource created with <span
class="function">socket_create.
返回值
This function returns a socket error code.
范例
示例 #1 socket_last_error example
<?php
$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg");
}
?>
注释
Note:
socket_last_error does not clear the error code, use socket_clear_error for this purpose.
socket_listen
Listens for a connection on a socket
说明
bool <span
class="methodname">socket_listen ( <span
class="methodparam">resource $socket
[, int
$backlog = 0 ] )
After the socket socket has been created using <span
class="function">socket_create and bound to a name with <span
class="function">socket_bind, it may be told to listen for
incoming connections on socket.
socket_listen is applicable only to
sockets of type SOCK_STREAM or SOCK_SEQPACKET.
参数
socket
A valid socket resource created with <span
class="function">socket_create or <span
class="function">socket_addrinfo_bind
backlog
A maximum of backlog incoming connections will be queued for
processing. If a connection request arrives with the queue full the
client may receive an error with an indication of ECONNREFUSED, or, if
the underlying protocol supports retransmission, the request may be
ignored so that retries may succeed.
Note:
The maximum number passed to the
backlogparameter highly depends on the underlying platform. On Linux, it is silently truncated toSOMAXCONN. On win32, if passedSOMAXCONN, the underlying service provider responsible for the socket will set the backlog to a maximum reasonable value. There is no standard provision to find out the actual backlog value on this platform.
返回值
成功时返回 true, 或者在失败时返回 false。 The error code
can be retrieved with socket_last_error.
This code may be passed to <span
class="function">socket_strerror to get a textual explanation of
the error.
参见
- socket_accept
- socket_bind
- socket_connect
- socket_create
- socket_strerror
- socket_addrinfo_bind
socket_read
Reads a maximum of length bytes from a socket
说明
string <span
class="methodname">socket_read ( <span
class="type">resource $socket , <span
class="methodparam">int $length [,
int $type<span
class="initializer"> = PHP_BINARY_READ ] )
The function socket_read reads from the
socket resource socket created by the <span
class="function">socket_create or <span
class="function">socket_accept functions.
参数
socket
A valid socket resource created with <span
class="function">socket_create or <span
class="function">socket_accept.
length
The maximum number of bytes read is specified by the length parameter.
Otherwise you can use \r, \n, or \0 to end reading
(depending on the type parameter, see below).
type
Optional type parameter is a named constant:
-
PHP_BINARY_READ(Default) - use the system recv() function. Safe for reading binary data. -
PHP_NORMAL_READ- reading stops at \n or \r.
返回值
socket_read returns the data as a string
on success, or false on error (including if the remote host has
closed the connection). The error code can be retrieved with <span
class="function">socket_last_error. This code may be passed to
socket_strerror to get a textual
representation of the error.
Note:
socket_read returns a zero length string ("") when there is no more data to read.
参见
- socket_accept
- socket_bind
- socket_connect
- socket_listen
- socket_last_error
- socket_strerror
- socket_write
socket_recv
从已连接的socket接收数据
说明
int <span
class="methodname">socket_recv ( <span
class="type">resource $socket , <span
class="methodparam">string &$buf ,
int $len ,
int $flags
)
函数 socket_recv 从 socket 中接受长度为
len 字节的数据,并保存在 buf 中。 <span
class="function">socket_recv
用于从已连接的socket中接收数据。除此之外,可以设定一个或多个 flags
来控制函数的具体行为。
buf 以引用形式传递,因此必须是一个以声明的有效的变量。从 socket
中接收到的数据将会保存在 buf 中。
参数
socket
参数 socket 必须是一个由 socket_create
创建的socket资源。
buf
从socket中获取的数据将被保存在由 buf 制定的变量中。
如果有错误发生,如链接被重置,数据不可用等等, buf 将被设为
null。
len
长度最多为 len 字节的数据将被接收。
flags
flags 的值可以为下列任意flag的组合。使用按位或运算符(|)来
组合不同的flag。
| Flag | 描述 |
|---|---|
MSG_OOB |
处理超出边界的数据 |
MSG_PEEK |
从接受队列的起始位置接收数据,但不将他们从接受队列中移除。 |
MSG_WAITALL |
在接收到至少 len 字节的数据之前,造成一个阻塞,并暂停脚本运行(block)。但是, 如果接收到中断信号,或远程服务器断开连接,该函数将返回少于 len 字节的数据。 |
MSG_DONTWAIT |
如果制定了该flag,函数将不会造成阻塞,即使在全局设置中指定了阻塞设置。 |
返回值
socket_recv
返回一个数字,表示接收到的字节数。如果发生了错误,则返回 false
错误码可使用 socket_last_error
接收。也可使用函数 socket_strerror
来取得关于错误的文字描述。
范例
示例 #1 socket_recv 范例
该范例简单地使用 socket_recv 重写了 范例 中的 第一个例子。
<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
echo "Reading response:\n\n";
$buf = 'This is my buffer.';
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($socket);
echo $buf . "\n";
echo "OK.\n\n";
?>
The above example will produce something like:
<h2>TCP/IP Connection</h2>
OK.
Attempting to connect to '208.77.188.166' on port '80'...OK.
Sending HTTP HEAD request...OK.
Reading response:
Read 123 bytes from socket_recv(). Closing socket...HTTP/1.1 200 OK
Date: Mon, 14 Sep 2009 08:56:36 GMT
Server: Apache/2.2.3 (Red Hat)
Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
ETag: "b80f4-1b6-80bfd280"
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8
OK.
socket_recvfrom
Receives data from a socket whether or not it is connection-oriented
说明
int <span
class="methodname">socket_recvfrom ( <span
class="methodparam">resource $socket
, string
&$buf , int
$len , int
$flags , <span
class="type">string &$name [, <span
class="methodparam">int &$port ] )
The socket_recvfrom function receives
len bytes of data in buf from name on port port (if the socket
is not of type AF_UNIX) using socket. <span
class="function">socket_recvfrom can be used to gather data from
both connected and unconnected sockets. Additionally, one or more flags
can be specified to modify the behaviour of the function.
The name and port must be passed by reference. If the socket is not
connection-oriented, name will be set to the internet protocol address
of the remote host or the path to the UNIX socket. If the socket is
connection-oriented, name is null. Additionally, the port will
contain the port of the remote host in the case of an unconnected
AF_INET or AF_INET6 socket.
Note: 此函数可安全用于二进制对象。
参数
socket
The socket must be a socket resource previously created by
socket_create().
buf
The data received will be fetched to the variable specified with buf.
len
Up to len bytes will be fetched from remote host.
flags
The value of flags can be any combination of the following flags,
joined with the binary OR (|) operator.
| Flag | Description |
|---|---|
MSG_OOB |
Process out-of-band data. |
MSG_PEEK |
Receive data from the beginning of the receive queue without removing it from the queue. |
MSG_WAITALL |
Block until at least len are received. However, if a signal is caught or the remote host disconnects, the function may return less data. |
MSG_DONTWAIT |
With this flag set, the function returns even if it would normally have blocked. |
name
If the socket is of the type AF_UNIX type, name is the path to
the file. Else, for unconnected sockets, name is the IP address of,
the remote host, or null if the socket is connection-oriented.
port
This argument only applies to AF_INET and AF_INET6 sockets,
and specifies the remote port from which the data is received. If the
socket is connection-oriented, port will be null.
返回值
socket_recvfrom returns the number of
bytes received, or false if there was an error. The actual error
code can be retrieved by calling <span
class="function">socket_last_error. This error code may be
passed to socket_strerror to get a
textual explanation of the error.
范例
示例 #1 socket_recvfrom example
<?php
error_reporting(E_ALL | E_STRICT);
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, '127.0.0.1', 1223);
$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 12, 0, $from, $port);
echo "Received $buf from remote address $from and remote port $port" . PHP_EOL;
?>
This example will initiate a UDP socket on port 1223 of 127.0.0.1 and print at most 12 characters received from a remote host.
参见
- socket_recv
- socket_send
- socket_sendto
- socket_create
socket_recvmsg
Read a message
说明
int <span
class="methodname">socket_recvmsg ( <span
class="methodparam">resource $socket
, array
&$message [, <span
class="type">int $flags =
0 ] )
Warning
本函数还未编写文档,仅有参数列表。
参数
socket
message
flags
返回值
参见
- socket_sendmsg
- socket_cmsg_space
socket_select
Runs the select() system call on the given arrays of sockets with a specified timeout
说明
int <span
class="methodname">socket_select ( <span
class="methodparam">array &$read ,
array
&$write , <span
class="type">array &$except , <span
class="methodparam">int $tv_sec [,
int $tv_usec<span
class="initializer"> = 0 ] )
socket_select accepts arrays of sockets and waits for them to change status. Those coming with BSD sockets background will recognize that those socket resource arrays are in fact the so-called file descriptor sets. Three independent arrays of socket resources are watched.
参数
read
The sockets listed in the read array will be watched to see if
characters become available for reading (more precisely, to see if a
read will not block - in particular, a socket resource is also ready on
end-of-file, in which case a socket_read
will return a zero length string).
write
The sockets listed in the write array will be watched to see if a
write will not block.
except
The sockets listed in the except array will be watched for exceptions.
tv_sec
The tv_sec and tv_usec together form the timeout parameter. The
timeout is an upper bound on the amount of time elapsed before <span
class="function">socket_select return. tv_sec may be zero ,
causing socket_select to return
immediately. This is useful for polling. If tv_sec is null (no
timeout), socket_select can block
indefinitely.
tv_usec
Warning
On exit, the arrays are modified to indicate which socket resource actually changed status.
You do not need to pass every array to <span
class="function">socket_select. You can leave it out and use an
empty array or null instead. Also do not forget that those arrays
are passed by reference and will be modified after <span
class="function">socket_select returns.
Note:
Due a limitation in the current Zend Engine it is not possible to pass a constant modifier like
nulldirectly as a parameter to a function which expects this parameter to be passed by reference. Instead use a temporary variable or an expression with the leftmost member being a temporary variable:示例 #1 Using
nullwith <span class="function">socket_select<?php $e = NULL; socket_select($r, $w, $e, 0); ?>
返回值
On success socket_select returns the
number of socket resources contained in the modified arrays, which may
be zero if the timeout expires before anything interesting happens. On
error false is returned. The error code can be retrieved with
socket_last_error.
Note:
Be sure to use the === operator when checking for an error. Since the socket_select may return 0 the comparison with == would evaluate to
true:示例 #2 Understanding <span class="function">socket_select's result
<?php $e = NULL; if (false === socket_select($r, $w, $e, 0)) { echo "socket_select() failed, reason: " . socket_strerror(socket_last_error()) . "\n"; } ?>
范例
示例 #3 socket_select example
<?php
/* Prepare the read array */
$read = array($socket1, $socket2);
$write = NULL;
$except = NULL;
$num_changed_sockets = socket_select($read, $write, $except, 0);
if ($num_changed_sockets === false) {
/* Error handling */
} else if ($num_changed_sockets > 0) {
/* At least at one of the sockets something interesting happened */
}
?>
注释
Note:
Be aware that some socket implementations need to be handled very carefully. A few basic rules:
- You should always try to use <span class="function">socket_select without timeout. Your program should have nothing to do if there is no data available. Code that depends on timeouts is not usually portable and difficult to debug.
- No socket resource must be added to any set if you do not intend to check its result after the <span class="function">socket_select call, and respond appropriately. After socket_select returns, all socket resources in all arrays must be checked. Any socket resource that is available for writing must be written to, and any socket resource available for reading must be read from.
- If you read/write to a socket returns in the arrays be aware that they do not necessarily read/write the full amount of data you have requested. Be prepared to even only be able to read/write a single byte.
- It's common to most socket implementations that the only exception caught with the
exceptarray is out-of-bound data received on a socket.
参见
- socket_read
- socket_write
- socket_last_error
- socket_strerror
socket_send
Sends data to a connected socket
说明
int <span
class="methodname">socket_send ( <span
class="type">resource $socket , <span
class="methodparam">string $buf ,
int $len ,
int $flags
)
The function socket_send sends len
bytes to the socket socket from buf.
参数
socket
A valid socket resource created with <span
class="function">socket_create or <span
class="function">socket_accept.
buf
A buffer containing the data that will be sent to the remote host.
len
The number of bytes that will be sent to the remote host from buf.
flags
The value of flags can be any combination of the following flags,
joined with the binary OR (|) operator.
MSG_OOB |
Send OOB (out-of-band) data. |
MSG_EOR |
Indicate a record mark. The sent data completes the record. |
MSG_EOF |
Close the sender side of the socket and include an appropriate notification of this at the end of the sent data. The sent data completes the transaction. |
MSG_DONTROUTE |
Bypass routing, use direct interface. |
返回值
socket_send returns the number of bytes
sent, or false on error.
参见
- socket_sendto
socket_sendmsg
Send a message
说明
int<span
class="type">false <span
class="methodname">socket_sendmsg ( <span
class="methodparam">resource $socket
, array
$message [, <span
class="type">int $flags =
0 ] )
Warning
本函数还未编写文档,仅有参数列表。
参数
socket
message
flags
返回值
Returns the number of bytes sent, 或者在失败时返回 false.
参见
- socket_recvmsg
- socket_cmsg_space
socket_sendto
Sends a message to a socket, whether it is connected or not
说明
int <span
class="methodname">socket_sendto ( <span
class="methodparam">resource $socket
, string
$buf , int
$len , int
$flags , <span
class="type">string $addr [, <span
class="methodparam">int $port<span
class="initializer"> = 0 ] )
The function socket_sendto sends len
bytes from buf through the socket socket to the port at the
address addr.
参数
socket
A valid socket resource created using <span
class="function">socket_create.
buf
The sent data will be taken from buffer buf.
len
len bytes from buf will be sent.
flags
The value of flags can be any combination of the following flags,
joined with the binary OR (|) operator.
MSG_OOB |
Send OOB (out-of-band) data. |
MSG_EOR |
Indicate a record mark. The sent data completes the record. |
MSG_EOF |
Close the sender side of the socket and include an appropriate notification of this at the end of the sent data. The sent data completes the transaction. |
MSG_DONTROUTE |
Bypass routing, use direct interface. |
addr
IP address of the remote host.
port
port is the remote port number at which the data will be sent.
返回值
socket_sendto returns the number of bytes
sent to the remote host, or false if an error occurred.
范例
示例 #1 socket_sendto Example
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$msg = "Ping !";
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, '127.0.0.1', 1223);
socket_close($sock);
?>
参见
- socket_send
socket_set_block
Sets blocking mode on a socket resource
说明
bool <span
class="methodname">socket_set_block ( <span
class="methodparam">resource $socket
)
The socket_set_block function removes
the O_NONBLOCK flag on the socket specified by the socket
parameter.
When an operation (e.g. receive, send, connect, accept, ...) is performed on a blocking socket, the script will pause its execution until it receives a signal or it can perform the operation.
参数
socket
A valid socket resource created with <span
class="function">socket_create or <span
class="function">socket_accept.
返回值
成功时返回 true, 或者在失败时返回 false。
范例
示例 #1 socket_set_block example
<?php
$socket = socket_create_listen(1223);
socket_set_block($socket);
socket_accept($socket);
?>
This example creates a listening socket on all interfaces on port 1223
and sets the socket to O_BLOCK mode. <span
class="function">socket_accept will hang until there is a
connection to accept.
参见
- socket_set_nonblock
- socket_set_option
socket_set_nonblock
Sets nonblocking mode for file descriptor fd
说明
bool <span
class="methodname">socket_set_nonblock ( <span
class="methodparam">resource $socket
)
The socket_set_nonblock function sets
the O_NONBLOCK flag on the socket specified by the socket
parameter.
When an operation (e.g. receive, send, connect, accept, ...) is performed on a non-blocking socket, the script will not pause its execution until it receives a signal or it can perform the operation. Rather, if the operation would result in a block, the called function will fail.
参数
socket
A valid socket resource created with <span
class="function">socket_create or <span
class="function">socket_accept.
返回值
成功时返回 true, 或者在失败时返回 false。
范例
示例 #1 socket_set_nonblock example
<?php
$socket = socket_create_listen(1223);
socket_set_nonblock($socket);
socket_accept($socket);
?>
This example creates a listening socket on all interfaces on port 1223
and sets the socket to O_NONBLOCK mode. <span
class="function">socket_accept will immediately fail unless
there is a pending connection exactly at this moment.
参见
- socket_set_block
- socket_set_option
- stream_set_blocking
socket_set_option
Sets socket options for the socket
说明
bool <span
class="methodname">socket_set_option ( <span
class="methodparam">resource $socket
, int
$level , <span
class="type">int $optname , <span
class="methodparam">mixed $optval )
The socket_set_option function sets the
option specified by the optname parameter, at the specified protocol
level, to the value pointed to by the optval parameter for the
socket.
参数
socket
A valid socket resource created with <span
class="function">socket_create or <span
class="function">socket_accept.
level
The level parameter specifies the protocol level at which the option
resides. For example, to retrieve options at the socket level, a level
parameter of SOL_SOCKET would be used. Other levels, such as TCP,
can be used by specifying the protocol number of that level. Protocol
numbers can be found by using the <span
class="function">getprotobyname function.
optname
The available socket options are the same as those for the <span
class="function">socket_get_option function.
optval
The option value.
返回值
成功时返回 true, 或者在失败时返回 false。
范例
示例 #1 socket_set_option example
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!is_resource($socket)) {
echo 'Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}
if (!socket_bind($socket, '127.0.0.1', 1223)) {
echo 'Unable to bind socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
}
$rval = socket_get_option($socket, SOL_SOCKET, SO_REUSEADDR);
if ($rval === false) {
echo 'Unable to get socket option: '. socket_strerror(socket_last_error()) . PHP_EOL;
} else if ($rval !== 0) {
echo 'SO_REUSEADDR is set on socket !' . PHP_EOL;
}
?>
参见
- socket_create
- socket_bind
- socket_strerror
- socket_last_error
- socket_get_option
socket_setopt
别名 socket_set_option
说明
此函数是该函数的别名: <span class="function">socket_set_option.
socket_shutdown
Shuts down a socket for receiving, sending, or both
说明
bool <span
class="methodname">socket_shutdown ( <span
class="methodparam">resource $socket
[, int $how<span
class="initializer"> = 2 ] )
The socket_shutdown function allows you
to stop incoming, outgoing or all data (the default) from being sent
through the socket
Note:
The associated buffer, or buffers, may or may not be emptied.
参数
socket
A valid socket resource created with <span
class="function">socket_create.
how
The value of how can be one of the following:
| 0 | Shutdown socket reading |
| 1 | Shutdown socket writing |
| 2 | Shutdown socket reading and writing |
返回值
成功时返回 true, 或者在失败时返回 false。
socket_strerror
Return a string describing a socket error
说明
string <span
class="methodname">socket_strerror ( <span
class="methodparam">int $errno )
socket_strerror takes as its errno
parameter a socket error code as returned by <span
class="function">socket_last_error and returns the
corresponding explanatory text.
Note:
Although the error messages generated by the socket extension are in English, the system messages retrieved with this function will appear depending on the current locale (
LC_MESSAGES).
参数
errno
A valid socket error number, likely produced by <span
class="function">socket_last_error.
返回值
Returns the error message associated with the errno parameter.
范例
示例 #1 socket_strerror example
<?php
if (false == ($socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
if (false == (@socket_bind($socket, '127.0.0.1', 80))) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
?>
The expected output from the above example (assuming the script is not run with root privileges):
socket_bind() failed: reason: Permission denied
参见
- socket_accept
- socket_bind
- socket_connect
- socket_listen
- socket_create
socket_write
Write to a socket
说明
int<span
class="type">false <span
class="methodname">socket_write ( <span
class="methodparam">resource $socket
, string
$buffer [, <span
class="type">int $length =
0 ] )
The function socket_write writes to the
socket from the given buffer.
参数
socket
buffer
The buffer to be written.
length
The optional parameter length can specify an alternate length of bytes
written to the socket. If this length is greater than the buffer length,
it is silently truncated to the length of the buffer.
返回值
Returns the number of bytes successfully written to the socket
或者在失败时返回 false. The error code can be retrieved with <span
class="function">socket_last_error. This code may be passed to
socket_strerror to get a textual
explanation of the error.
Note:
It is perfectly valid for socket_write to return zero which means no bytes have been written. Be sure to use the === operator to check for
falsein case of an error.
注释
Note:
socket_write does not necessarily write all bytes from the given buffer. It's valid that, depending on the network buffers etc., only a certain amount of data, even one byte, is written though your buffer is greater. You have to watch out so you don't unintentionally forget to transmit the rest of your data.
参见
- socket_accept
- socket_bind
- socket_connect
- socket_listen
- socket_read
- socket_strerror
socket_wsaprotocol_info_export
Exports the WSAPROTOCOL_INFO Structure
说明
string<span
class="type">false <span
class="methodname">socket_wsaprotocol_info_export ( <span
class="methodparam">resource $socket
, int
$target_pid )
Exports the WSAPROTOCOL_INFO structure into shared memory and returns
an identifier to be used with <span
class="function">socket_wsaprotocol_info_import. The exported
ID is only valid for the given target_pid.
Note: This function is available only on Windows.
参数
socket
A valid socket resource.
target_pid
The ID of the process which will import the socket.
返回值
Returns an identifier to be used for the import, 或者在失败时返回
false
参见
- socket_wsaprotocol_info_import
- socket_wsaprotocol_info_release
socket_wsaprotocol_info_import
Imports a Socket from another Process
说明
resource<span
class="type">false <span
class="methodname">socket_wsaprotocol_info_import ( <span
class="methodparam">string $info_id )
Imports a socket which has formerly been exported from another process.
Note: This function is available only on Windows.
参数
info_id
The ID which has been returned by a former call to <span
class="function">socket_wsaprotocol_info_export.
返回值
Returns the socket resource, 或者在失败时返回 false
参见
- socket_wsaprotocol_info_export
socket_wsaprotocol_info_release
Releases an exported WSAPROTOCOL_INFO Structure
说明
bool <span
class="methodname">socket_wsaprotocol_info_release ( <span
class="methodparam">string $info_id )
Releases the shared memory corresponding to the given info_id.
Note: This function is available only on Windows.
参数
info_id
The ID which has been returned by a former call to <span
class="function">socket_wsaprotocol_info_export.
返回值
成功时返回 true, 或者在失败时返回 false。
参见
- socket_wsaprotocol_info_export
目录
- socket_accept — Accepts a connection on a socket
- socket_addrinfo_bind — Create and bind to a socket from a given addrinfo
- socket_addrinfo_connect — Create and connect to a socket from a given addrinfo
- socket_addrinfo_explain — Get information about addrinfo
- socket_addrinfo_lookup — Get array with contents of getaddrinfo about the given hostname
- socket_bind — 给套接字绑定名字
- socket_clear_error — 清除套接字或者最后的错误代码上的错误
- socket_close — 关闭套接字资源
- socket_cmsg_space — Calculate message buffer size
- socket_connect — 开启一个套接字连接
- socket_create_listen — Opens a socket on port to accept connections
- socket_create_pair — Creates a pair of indistinguishable sockets and stores them in an array
- socket_create — 创建一个套接字(通讯节点)
- socket_export_stream — Export a socket extension resource into a stream that encapsulates a socket
- socket_get_option — Gets socket options for the socket
- socket_getopt — 别名 socket_get_option
- socket_getpeername — 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
- socket_getsockname — 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
- socket_import_stream — Import a stream
- socket_last_error — Returns the last error on the socket
- socket_listen — Listens for a connection on a socket
- socket_read — Reads a maximum of length bytes from a socket
- socket_recv — 从已连接的socket接收数据
- socket_recvfrom — Receives data from a socket whether or not it is connection-oriented
- socket_recvmsg — Read a message
- socket_select — Runs the select() system call on the given arrays of sockets with a specified timeout
- socket_send — Sends data to a connected socket
- socket_sendmsg — Send a message
- socket_sendto — Sends a message to a socket, whether it is connected or not
- socket_set_block — Sets blocking mode on a socket resource
- socket_set_nonblock — Sets nonblocking mode for file descriptor fd
- socket_set_option — Sets socket options for the socket
- socket_setopt — 别名 socket_set_option
- socket_shutdown — Shuts down a socket for receiving, sending, or both
- socket_strerror — Return a string describing a socket error
- socket_write — Write to a socket
- socket_wsaprotocol_info_export — Exports the WSAPROTOCOL_INFO Structure
- socket_wsaprotocol_info_import — Imports a Socket from another Process
- socket_wsaprotocol_info_release — Releases an exported WSAPROTOCOL_INFO Structure