phpday 2027 - Call for Papers

socket_getpeername

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

socket_getpeername — Interroga il lato remoto del socket indicato

Descrizione

function socket_getpeername(Socket $socket, string &$address, int &$port = null): bool

Interroga il lato remoto del socket indicato, ottenendo come risultato host/porta oppure un percorso del filesystem Unix, a seconda del suo tipo.

Elenco dei parametri

socket

Un'istanza di Socket creata con socket_create() o socket_accept().

address

Se il socket indicato è di tipo AF_INET o AF_INET6, socket_getpeername() restituirà l'indirizzo IP del peer (remoto) nella notazione appropriata (ad es. 127.0.0.1 o fe80::1) nel parametro address e, se è presente il parametro opzionale port, anche la porta associata.

Se il socket indicato è di tipo AF_UNIX, socket_getpeername() restituirà il percorso del filesystem Unix (ad es. /var/run/daemon.sock) nel parametro address.

port

Se indicato, conterrà la porta associata a address.

Valori restituiti

Restituisce true in caso di successo o false in caso di fallimento. socket_getpeername() può anche restituire false se il tipo del socket non è nessuno tra AF_INET, AF_INET6 e AF_UNIX; in tal caso l'ultimo codice di errore del socket non viene aggiornato.

Log delle modifiche

Versione Descrizione
8.0.0 socket ora è un'istanza di Socket; in precedenza era una resource.

Note

Nota:

socket_getpeername() non dovrebbe essere utilizzata con socket AF_UNIX creati con socket_accept(). Solo i socket creati con socket_connect() o un socket server primario dopo una chiamata a socket_bind() restituiranno valori significativi.

Nota:

Affinché socket_getpeername() restituisca un valore significativo, il socket a cui viene applicata deve naturalmente essere uno per cui il concetto di "peer" abbia senso.

Vedere anche:

  • socket_getsockname() - Interroga il lato locale del socket indicato, ottenendo come risultato host/porta oppure un percorso del filesystem Unix, a seconda del suo tipo
  • socket_last_error() - Restituisce l'ultimo errore sul socket
  • socket_strerror() - Restituisce una stringa che descrive un errore del socket

+add a note

User Contributed Notes 2 notes

up
3
redph0enix at hotmail dot com
23 years ago
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";
up
2
Anonymous
10 years ago
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.
To Top