array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.socket-write.php', 1 => 'socket_write', ), 'up' => array ( 0 => 'ref.sockets.php', 1 => 'Socket 函数', ), 'prev' => array ( 0 => 'function.socket-strerror.php', 1 => 'socket_strerror', ), 'next' => array ( 0 => 'function.socket-wsaprotocol-info-export.php', 1 => 'socket_wsaprotocol_info_export', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/sockets/functions/socket-write.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)
socket_write — 向套接字写数据
函数 socket_write() 向 socket
写入 data
。
socket
data
要写入到缓冲区的数据。
length
可选参数 length
可以指定写入 socket
的字节长度。如果写入的字节长度大于缓冲区长度,默认将被截取为缓冲区长度。
返回成功写入 socket 的字节数 或者在失败时返回 false
。可以通过调用 socket_last_error()
来检索实际的错误码。将错误码作为参数传递给 socket_strerror() 以获得错误的文本解释。
注意:
对于 socket_wirte() 来说返回 0 是完全有效的,这代表没有字节被写入。如果发生错误,务必使用
===
运算符来判断是否为false
。
注意:
socket_write() 不一定会写入缓冲区全部的内容。根据网络缓冲区的条件,即使缓冲区待写入的数据很多,也可能只写入一部分,甚至是一个字节。因此,必须注意是否所有数据都写入成功。