CakeFest 2024: The Official CakePHP Conference

socket_set_nonblock

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

socket_set_nonblockEstablece el modo de no-bloqueo para el descriptor de archivo fd

Descripción

socket_set_nonblock(resource $socket): bool

La funciòn socket_set_nonblock() establece la bandera O_NONBLOCK en el socket especificado por el parámetro socket.

Cuando una operación (p.ej. recibir, enviar, conectar, aceptar, ...) se lleva a cabo sobre un socket de no-bloqueo, el script no pausará su ejecución hasta que reciba una señal o pueda realizar la operación. Más bien, si la operacion resultaría en un bloqueo, la llamada a la función fallará.

Parámetros

socket

Un recurso socket válido creado con socket_create() o socket_accept().

Valores devueltos

Devuelve true en caso de éxito o false en caso de error.

Ejemplos

Ejemplo #1 Un ejemplo de socket_set_nonblock()

<?php
$socket
= socket_create_listen(1223);
socket_set_nonblock($socket);

socket_accept($socket);
?>

Este ejemplo crea un socket de escucha en todas las interfaces en el puerto 1223 y establece el socket al modo O_NONBLOCK. socket_accept() fallará inmediantamente a menos que haya una conexión pendiente en ese preciso momento.

Ver también

add a note

User Contributed Notes 1 note

up
3
kpobococ at gmail dot com
14 years ago
Beware, when using this function within a loop (i.e. a demon with a socket). The socket_accept(), for example, emits a warning each time there is no incoming connection available to be read. My php error log file got huge in a matter of seconds, eventually crashing the server.

Of course, i used the @ before the function to take care of that problem.

[EDITOR: One can (and should) use socket_select to detect a new connection on a socket (it's a "readable" event)]
To Top