The reason it won't work for UDP is that UDP is stateless; logically there are no peers other than at the time a packet is sent or received. Or more strictly, a UDP socket can interact with 0..N peers.
(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)
socket_getpeername — Interroga il lato remoto di un dato socket per ottenere o la combinazione host/porta od un percorso Unix, in base al tipo di socket
Questa funzione è SPERIMENTALE. Ovvero, il comportamento di questa funzione, il nome di questa funzione, in definitiva tutto ciò che è documentato qui può cambiare nei futuri rilasci del PHP senza preavviso. Siete avvisati, l'uso di questa funzione è a vostro rischio.
Se il socket dato è di tipo AF_INET
oppure AF_INET6
,
socket_getpeername() restituisce l'indirizzo IP
remoto nella notazione appropriata (ad esempio
127.0.0.1
oppure fe80::1
) nel parametro indirizzo
e, se presente il parametro opzionale porta
,
anche la porta associata.
Se il socket dato è di tipo AF_UNIX
,
socket_getpeername() restituirà un percorso Unix
(ad esempio /var/run/daemon.sock
) nel
parametro indirizzo
.
Nota: La funzione socket_getpeername() non dovrebbe essere usata con socket
AF_UNIX
creati da socket_accept(). Soltanto i socket creati con socket_connect() o un socket server primario conseguente alla chiamata di socket_bind() restituirà dei valori significativi.
Restituisce true
in caso di successo, false
in caso di fallimento. socket_getpeername() può anche restituire
false
se il tipo di socket non è AF_INET
, AF_INET6
o
AF_UNIX
, in questo caso l'ultimo codice di errore del socket
non viene aggiornato.
Vedere anche socket_getsockname(), socket_last_error() e socket_strerror().
The reason it won't work for UDP is that UDP is stateless; logically there are no peers other than at the time a packet is sent or received. Or more strictly, a UDP socket can interact with 0..N peers.
socket_getpeername will not work for UDP sockets. Instead, use socket_recvfrom - it provides the IP address and port of the source server - eg:
$size=socket_recvfrom($socket,$input,65535,0,$ipaddress,$port);
echo "Received [$input] ($size bytes) from IP $ipaddress Port $port\n";