You can check the length of the resulting binary string simply using strlen :
$prefix = socket_read($socket, 4, PHP_BINARY_READ); if (strlen($prefix) != 4) { // not 4 bytes long }
According to your previous question, this binary string is a 32-bit length. Unpack as such (with the same format specifier that you use when packing it), then select the message and use strlen again to check the length:
$length = current(unpack('l', $prefix)); $message = socket_read($socket, $length, PHP_BINARY_READ); if (strlen($message) != $length) { // $message not the size of $length }
source share