socket_get_option

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

socket_get_optionOttiene le opzioni per un socket

Descrizione

socket_get_option(resource $socket, int $level, int $optname): mixed
Avviso

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.

La funzione socket_get_option() restituisce i valori per il parametro indicato in optname per il socket indicato da socket. La funzione restituisce false se non riesce.

Il parametro level specifica a quale livello di protocollo risiede l'opzione cercata. Ad esempio, per recuperare le informzioni su opzioni a livello di socket, il parametro level deve essere impostato a SOL_SOCKET. Altri livelli tipo TCP, possono essere utilizzati specificando il numero del livello. I numeri dei livelli dei protocolli possono essere ottenuti tramite getprotobyname().

Opzioni per i socket disponibili
Opzione Descrizione
SO_DEBUG Riporta informazioni per il debug.
SO_ACCEPTCONN Indica se il socket è abilitato in ascolto.
SO_BROADCAST Indica se sono supportate le trasmissioni dei messaggi di broadcast.
SO_REUSEADDR Riporta se gli indirizzi locali possono essere riutilizzati.
SO_KEEPALIVE Riporta se la connesisone deve essere mantenuta attiva tramite la trasmissione periodica di messaggi. Se il socket connesso non risponde a questi messaggi, la connessione viene interrotta ed i processi che stavano scrivendo in quel socket riceveranno il segnale SIGPIPE.
SO_LINGER Indice se il socket debba ritardare il socket_close() se vi sono dati.
SO_OOBINLINE Indica se il socket gestisce i dati fuori-banda.
SO_SNDBUF Riporta le dimensioni del buffer di trasmissione.
SO_RCVBUF Riporta le dimensioni del buffer di ricezione.
SO_ERROR Restituisce informaizoni sugli stati di errore e li ripulisce.
SO_TYPE Restituisce il tipo disocket.
SO_DONTROUTE Indica se i messaggi in uscita ignorano i parametri standard di routing.
SO_RCVLOWAT Indica il numero minimo di byte da processare da parte del socket per le operazioni di input (default 1).
SO_RCVTIMEO Tempo di timeout per le operazioni di input.
SO_SNDLOWAT Riporta il numero minimo di byte da processare da parte del socket per le operazioni di output.
SO_SNDTIMEO Indica il tempo di timeout specificando il tempo che una funzione di output resti bloccata in attesa di potere inviare i dati.

Nota:

Nelle versioni di PHP antecedenti la 4.3.0, questa funzione era chiamata socket_getopt().

add a note

User Contributed Notes 4 notes

up
3
recycling dot sp dot am at gmail dot com
13 years ago
Just 2 notes here:
- On UNIX, If SO_DEBUG is set, the php program needs an effective user id of 0.
- activating SO_OOBINLINE on a socket is equivalent to passing MSG_OOB flag to each recieving functions used with that socket (eg: socket_recv, socket_recvfrom).
up
4
Chad Lavoie
13 years ago
If using Unix Sockets, and you want to use SO_PEERCRED, you can use the number 17 for the optname (and SOL_SOCKET for the level). The PID of the connecting process will be returned.
up
1
prennings at gmail dot com
10 years ago
I was playing around with this option to use multiply socket connections with same hostname and same port (IRC). However the socket function needed for this is SO_REUSEPORT.

Though the majority of linux distro's does not have that yet officially implented in there distro's.

However for debian there is an patch that can be installed to get it working:

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=c617f398edd4db2b8567a28e899a88f8f574798d

it has some work but I got it working after a while (Noobie in debian) maybe some other people are facing the same problem as I was.
up
0
skydiablo at gmx dot net
2 years ago
to receive UDP DHCP packets on a dedicated interface, you have to use the undocumented option SO_BINDTODEVICE:

<?php
$socket
= socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

socket_set_option($socket, SOL_SOCKET, SO_BINDTODEVICE, 'eth1');
socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($socket, SOL_SOCKET, SO_REUSEPORT, 1);

socket_bind($socket, '255.255.255.255', 67);
while (
1) {
if (
$src = @socket_recv($socket, $data, 9999, 0)) {
echo
$data . PHP_EOL;
}
}
?>
To Top