PHP 8.3.4 Released!

socket_last_error

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

socket_last_errorLit la dernière erreur générée par un socket

Description

socket_last_error(?Socket $socket = null): int

Si une instance de Socket est passée à cette fonction, la dernière erreur qui aura été générée par ce socket sera retourné. Si socket est null, le dernier code d'erreur généré est retourné. Ce comportement est particulièrement pratique pour des fonctions comme socket_create() qui ne retournent pas de socket en cas d'échec, et socket_select() qui peut échouer sans raison directement liée au socket. Le code d'erreur peut être transmis à socket_strerror() qui retourne un message d'erreur lisible.

Si aucune erreur n'est survenue, ou si l'erreur a été supprimée avec la fonction socket_clear_error(), cette fonction retournera 0.

Liste de paramètres

socket

Une instance de Socket créée par socket_create().

Valeurs de retour

Retourne le code erreur associé au socket.

Historique

Version Description
8.0.0 socket est désormais une instance de Socket ; auparavant, c'était une resource.
8.0.0 socket est désormais nullable.

Exemples

Exemple #1 Exemple avec 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(
"Impossible de créer le socket : [$errorcode] $errormsg");
}
?>

Notes

Note:

socket_last_error() n'efface pas le code d'erreur. Utilisez plutôt la fonction socket_clear_error() pour cela.

add a note

User Contributed Notes 2 notes

up
13
ca at php dot spamtrak dot org
14 years ago
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 */
?>
up
1
divinity76 at gmail dot com
5 years ago
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)
To Top