PHP Conference Japan 2024

socket_last_error

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

socket_last_error傳回 socket 上的最後一個錯誤

說明

socket_last_error(?Socket $socket = null): int

如果將 Socket 實例傳遞給此函式,則會返回此特定 socket 上發生的最後一個錯誤。如果 socketnull,則會返回最後一個失敗的 socket 函式的錯誤碼。後者對於像 socket_create() 這種在失敗時不返回 socket 的函式,以及像 socket_select() 這種可能由於與特定 socket 無直接關係的原因而失敗的函式特別有用。此錯誤碼適用於傳遞給 socket_strerror() 函式,該函式會返回描述給定錯誤碼的字串。

如果沒有發生錯誤,或者錯誤已透過 socket_clear_error() 清除,則此函式會返回 0

參數

socket

使用 socket_create() 建立的 Socket 實例。

返回值

此函式返回 socket 錯誤碼。

更新日誌

版本 說明
8.0.0 socket 現在是 Socket 實例;以前,它是 資源
8.0.0 socket 現在可以為 null。

範例

範例 #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(
"無法建立 socket: [$errorcode] $errormsg");
}
?>

注意事項

注意:

socket_last_error() 不會清除錯誤碼,請使用 socket_clear_error() 來清除錯誤碼。

新增註解

使用者貢獻的註解 2 則註解

13
ca at php dot spamtrak dot org
14 年前
這有點長,但我個人比較喜歡在我的程式碼中使用標準 C 定義。

<?php

define
('ENOTSOCK', 88); /* Socket operation on non-socket */
define('EDESTADDRREQ', 89); /* Destination address required */
define('EMSGSIZE', 90); /* Message too long */
define('EPROTOTYPE', 91); /* Protocol wrong type for socket */
define('ENOPROTOOPT', 92); /* Protocol not available */
define('EPROTONOSUPPORT', 93); /* Protocol not supported */
define('ESOCKTNOSUPPORT', 94); /* Socket type not supported */
define('EOPNOTSUPP', 95); /* Operation not supported on transport endpoint */
define('EPFNOSUPPORT', 96); /* Protocol family not supported */
define('EAFNOSUPPORT', 97); /* Address family not supported by protocol */
define('EADDRINUSE', 98); /* Address already in use */
define('EADDRNOTAVAIL', 99); /* Cannot assign requested address */
define('ENETDOWN', 100); /* Network is down */
define('ENETUNREACH', 101); /* Network is unreachable */
define('ENETRESET', 102); /* Network dropped connection because of reset */
define('ECONNABORTED', 103); /* Software caused connection abort */
define('ECONNRESET', 104); /* Connection reset by peer */
define('ENOBUFS', 105); /* No buffer space available */
define('EISCONN', 106); /* Transport endpoint is already connected */
define('ENOTCONN', 107); /* Transport endpoint is not connected */
define('ESHUTDOWN', 108); /* Cannot send after transport endpoint shutdown */
define('ETOOMANYREFS', 109); /* Too many references: cannot splice */
define('ETIMEDOUT', 110); /* Connection timed out */
define('ECONNREFUSED', 111); /* Connection refused */
define('EHOSTDOWN', 112); /* Host is down */
define('EHOSTUNREACH', 113); /* No route to host */
define('EALREADY', 114); /* Operation already in progress */
define('EINPROGRESS', 115); /* Operation now in progress */
define('EREMOTEIO', 121); /* Remote I/O error */
define('ECANCELED', 125); /* Operation Canceled */
?>
1
divinity76 at gmail dot com
5 年前
請注意,`socket_last_error()` 會快取最後一次 socket 系統呼叫的錯誤,它實際上並不會向作業系統查詢 socket 上的最後一個錯誤。因此,如果一個非同步 socket 在最後一次非同步操作成功啟動後發生錯誤,`socket_last_error()` 並不會知道,但 `socket_get_option($sock, SOL_SOCKET, SO_ERROR)` 會實際查詢作業系統,或者看起來是這樣...在 Windows 7 x64 SP1 上使用 Cygwin 的 PHP 7.1.16 版搭配非阻塞式 socket 觀察到的情況,`socket_last_error()` 從未察覺到目前的錯誤已從 EINPROGRESS(非阻塞式連線)變為 0(連線成功)。
To Top