Usage example for MSG_PEEK: this function tells if the socket has data available to be read, but preserving it to be read at a future moment.
<?php
// Workaround for the missing define
if(!defined('MSG_DONTWAIT')) define('MSG_DONTWAIT', 0x40);
// Function to check if there is data available in the socket
function SocketHasData($socket) {
// Based on the following fact:
// $result=0 -> disconnected, $result=false -> no data
$data = ''; // We need a buffer, but we won't use it
// MSG_PEEK means to preserve data in the queue, so it can
// actually be read afterwards
$result = socket_recv($socket, $data, 1, MSG_PEEK | MSG_DONTWAIT );
if ($result === false) return false; // If no data, returns false
return true; // Otherwise returns true
}
?>