The underlying sockpair() function does only support AF_UNIX at least on BSD and Linux.(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)
socket_create_pair — Crea una coppia di socket indistinguibili e li memorizza in un array
socket_create_pair() crea due socket connessi e
indistinguibili e li memorizza in pair.
Questa funzione è comunemente utilizzata nell'IPC (comunicazione tra processi).
domain
Il parametro domain specifica la famiglia di protocolli
che il socket deve utilizzare. Si veda socket_create()
per l'elenco completo.
type
Il parametro type seleziona il tipo di comunicazione
che il socket deve utilizzare. Si veda socket_create() per
l'elenco completo.
protocol
Il parametro protocol imposta lo specifico
protocollo all'interno del dominio domain indicato da utilizzare
nella comunicazione sul socket restituito. Il valore appropriato può essere ottenuto
a partire dal nome utilizzando getprotobyname(). Se
il protocollo desiderato è TCP o UDP, le costanti corrispondenti
SOL_TCP e SOL_UDP
possono anch'esse essere utilizzate.
Si veda socket_create() per l'elenco completo dei protocolli supportati.
pairRiferimento a un array in cui verranno inserite le due istanze di Socket.
| Versione | Descrizione |
|---|---|
| 8.0.0 |
pair ora è un riferimento a un array di istanze di Socket;
in precedenza era un riferimento a un array di resource.
|
Example #1 Esempio di socket_create_pair()
<?php
$sockets = array();
/* Su Windows occorre utilizzare AF_INET */
$domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX);
/* Configurazione della coppia di socket */
if (socket_create_pair($domain, SOCK_STREAM, 0, $sockets) === false) {
echo "socket_create_pair non riuscita. Motivo: ".socket_strerror(socket_last_error());
}
/* Invio e ricezione dei dati */
if (socket_write($sockets[0], "ABCdef123\n", strlen("ABCdef123\n")) === false) {
echo "socket_write() non riuscita. Motivo: ".socket_strerror(socket_last_error($sockets[0]));
}
if (($data = socket_read($sockets[1], strlen("ABCdef123\n"), PHP_BINARY_READ)) === false) {
echo "socket_read() non riuscita. Motivo: ".socket_strerror(socket_last_error($sockets[1]));
}
var_dump($data);
/* Chiusura dei socket */
socket_close($sockets[0]);
socket_close($sockets[1]);
?>
Example #2 Esempio di IPC con socket_create_pair()
<?php
$ary = array();
$strone = 'Messaggio dal padre.';
$strtwo = 'Messaggio dal figlio.';
if (socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $ary) === false) {
echo "socket_create_pair() non riuscita. Motivo: ".socket_strerror(socket_last_error());
}
$pid = pcntl_fork();
if ($pid == -1) {
echo 'Impossibile eseguire il fork del processo.';
} elseif ($pid) {
/*padre*/
socket_close($ary[0]);
if (socket_write($ary[1], $strone, strlen($strone)) === false) {
echo "socket_write() non riuscita. Motivo: ".socket_strerror(socket_last_error($ary[1]));
}
if (socket_read($ary[1], strlen($strtwo), PHP_BINARY_READ) == $strtwo) {
echo "Ricevuto $strtwo\n";
}
socket_close($ary[1]);
} else {
/*figlio*/
socket_close($ary[1]);
if (socket_write($ary[0], $strtwo, strlen($strtwo)) === false) {
echo "socket_write() non riuscita. Motivo: ".socket_strerror(socket_last_error($ary[0]));
}
if (socket_read($ary[0], strlen($strone), PHP_BINARY_READ) == $strone) {
echo "Ricevuto $strone\n";
}
socket_close($ary[0]);
}
?>
The underlying sockpair() function does only support AF_UNIX at least on BSD and Linux.