phpday 2027 - Call for Papers

socket_getsockname

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

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

Descrizione

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

Nota:

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

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_getsockname() restituirà l'indirizzo IP locale 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_getsockname() restituirà il percorso del filesystem Unix (ad es. /var/run/daemon.sock) nel parametro address.

port

Se fornito, conterrà la porta associata.

Valori restituiti

Restituisce true in caso di successo o false in caso di fallimento. socket_getsockname() 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.5.0 Ora recupera l'indice dell'interfaccia e la sua rappresentazione come stringa quando viene utilizzata su un socket della famiglia AF_PACKET.
8.0.0 socket ora è un'istanza di Socket; in precedenza era una resource.

Vedere anche:

User Contributed Notes 1 note

up
1
CXJ
12 years ago
Curiously, getsockname() works for socket_create() and socket_create_pair() Unix-domain (AF_UNIX) sockets if one calls socket_bind() after creation to name the formerly anonymous socket(s).  

Using a socket_bind() call also results in a file system "file" (socket, first character 's' in an "ls -l" listing) being created with the given name.  Such a "file" will need to be removed explicitly, as closing the socket will not remove it.
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top