A
A
ahwoobachairiesaas2014-09-12 21:16:41
PHP
ahwoobachairiesaas, 2014-09-12 21:16:41

How to overcome Warning: socket_create(): Unable to create socket [0]: An attempt was made to access a socket in a method forbidden by access rights?

I'm trying to ping.
The code:

function ping($host, $timeout = 1) {
    $socket = socket_create(AF_INET, SOCK_RAW, 1);
    if ($socket === false) {
        return array($err = socket_last_error(), socket_strerror($err));
    }
    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
    socket_connect($socket, $host, null);

    $ts = microtime(true);
    /* ICMP ping packet with a pre-calculated checksum */
    $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
    socket_send($socket, $package, strLen($package), 0);
    if (socket_read($socket, 255)) {
        $result = microtime(true) - $ts;
    } else {
        $result = false;
    }
    socket_close($socket);

    return $result;
}

Mistake:
PHP Warning: socket_create(): Unable to create socket [0]: An attempt was made to access
a socket using a method that is prohibited by access rights.
in C:\Users\Sergey\Documents\Sandbox\sitetest\sitetest.php on line 23

Warning: socket_create(): Unable to create socket [0]: An attempt was made to access a
socket using a method denied by access rights.
in C:\Users\Sergey\Documents\Sandbox\sitetest\sitetest.php on line 23
Ping failed: An attempt was made to access a socket using a method forbidden by access rights
.
(10013)

Why is an error returned?
OS - Windows 7 x64, firewall - turned off.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-09-12
@ahwoobachairiesaas

Because raw sockets can only be done from under the root / admin. Run the console with admin rights and run your script.

C
Cool Admin, 2014-09-12
@ifaustrue

Are sockets enabled in php.ini, is Apache configured correctly (is sockets.so enabled)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question