array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.socket-last-error.php', 1 => 'socket_last_error', ), 'up' => array ( 0 => 'ref.sockets.php', 1 => 'Socket 函数', ), 'prev' => array ( 0 => 'function.socket-import-stream.php', 1 => 'socket_import_stream', ), 'next' => array ( 0 => 'function.socket-listen.php', 1 => 'socket_listen', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/sockets/functions/socket-last-error.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_last_error — 返回套接字上的最后一个错误
如果将 Socket 实例传递给此函数,将会返回指定套接字发生的最后一个错误。如果
socket
为 null
, 则返回最后一个失败的套接字函数的错误码。此函数对以下情况特别有帮助:
socket_create() 失败时不会返回套接字,以及
socket_select() 失败的原因与特定套接字没有直接联系。
错误码适合提供给 socket_strerror(),将返回指定错误码的字符串描述。
如果没有错误或者已经使用 socket_clear_error() 清除了错误,则函数返回 0
。
此函数返回套接字错误码。
示例 #1 socket_last_error() 示例
<?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");
}
?>
注意:
socket_last_error() 不会清除错误代码,如果需要可以使用 socket_clear_error() 清除。