CakeFest 2024: The Official CakePHP Conference

socket_sendto

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

socket_sendtoInvia un messaggio ad un socket, a prescindere che sia connesso o meno

Descrizione

socket_sendto(
    resource $socket,
    string $buf,
    int $len,
    int $flags,
    string $addr,
    int $port = ?
): int
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_sendto() invia len bytes dal buffer buf attraverso il socket socket alla porta port dell'indirizzo addr

Il valore ammessi per flags può essere uno dei seguenti:

valori possibili per flags
0x1 Elabora dati OOB (fuori banda).
0x2 Preleva il messaggio in arrivo.
0x4 Ignora il routing, usa l'interfaccia diretta.
0x8 I dati completano il record.
0x100 I dati completano al transazione.

Example #1 Esempio di socket_sendto()

<?php
$sh
= socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (
socket_bind($sh, '127.0.0.1', 4242)) {
echo
"Socket agganciato correttamente";
}
$buf = 'Test Message';
$len = strlen($buf);
if (
socket_sendto($sh, $buf, $len, 0x100, '192.168.0.2', 4242) !== false) {
echo
"Messaggio inviato correttamente";
}
socket_close($sh);
?>

Vedere anche socket_send() e socket_sendmsg().

add a note

User Contributed Notes 1 note

up
1
ole_DOT_omland_AT_gmail_DOT_com
18 years ago
Here's how you can make an udp broadcast, useful sometimes, and does seem hard to figure out hwo to do..

<?php
$sock
= socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1);
socket_sendto($sock, $broadcast_string, strlen($broadcast_string), 0, '255.255.255.255', $port);
?>
To Top