nighthunter
Новичок
контроль соединения на сокетах
кусок кода
вопрос: как определить что соединение оборвалось? socket_read и socket_last_error никак не реагируют на обрыв.
кусок кода
PHP:
if (($this->sock = @socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror ($this->sock) . "\n"; exit(1);
}
socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1);
if (($ret = @socket_bind ($this->sock, $this->host, $this->port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror ($ret) . "\n"; exit(1);
}
if (($ret = @socket_listen ($this->sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror ($ret) . "\n"; exit(1);
}
socket_set_nonblock($this->sock);
while ($this->run) {
usleep($this->delay);
if (($msgsock = @socket_accept($this->sock))) {
socket_set_nonblock($msgsock);
socket_set_option($msgsock, SOL_SOCKET, SO_KEEPALIVE, 1);
}
if ($tmp = @socket_read($msgsock, 1024)) {
$buffer .= $tmp;
}
}