socket_last_error
(PHP 4 >= 4.1.0, PHP 5)
socket_last_error — Returns the last error on the socket
Description
$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 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.
Return Values
This function returns a socket error code.
Examples
Example #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");
}
?>
Notes
Note:
socket_last_error() does not clear the error code, use socket_clear_error() for this purpose.
- socket_accept
- socket_bind
- socket_clear_error
- socket_close
- socket_cmsg_space
- socket_connect
- socket_create_listen
- socket_create_pair
- socket_create
- socket_get_option
- socket_getopt
- socket_getpeername
- socket_getsockname
- socket_import_stream
- socket_last_error
- socket_listen
- socket_read
- socket_recv
- socket_recvfrom
- socket_recvmsg
- socket_select
- socket_send
- socket_sendmsg
- socket_sendto
- socket_set_block
- socket_set_nonblock
- socket_set_option
- socket_setopt
- socket_shutdown
- socket_strerror
- socket_write
Коментарии
This is a bit long, but personally I prefer to use the standard C defines in my code.
<?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 */
?>
note that socket_last_error() cache the last error from the last socket syscall, it does not actually query the OS for the last error on the socket, so if an async socket have an error after the last async operation started successfully, socket_last_error() doesn't know about it, but socket_get_option($sock, SOL_SOCKET, SO_ERROR) actually query the OS, or so it seems... observed on PHP 7.1.16 on Cygwin on win7 x64 SP1 with non-blocking sockets, socket_last_error() never caught on to the fact that the current error had changed from EINPROGRESS (non-blocking connect) to 0 (connection successful)