0

I'm trying to open a socket using php on a wamp server. I edited the php.ini file to accept sockets but it doesn't work. Is it because of my code or the wamp server installation?

<?php
error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = '192.168.1.53';
$port = 10000;
echo 'Entering';
if ($sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp")) == false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) .                "\n";
}

if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) .       "\n";
}

do {
if (($msgsock = socket_accept($sock)) === false) {
    echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock))        . "\n";
    break;
}
/* Send instructions. */
$msg = "\nWelcome to the PHP Test Server. \n" .
    "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));

do {
    if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
        echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
        break 2;
    }
    if (!$buf = trim($buf)) {
        continue;
    }
    if ($buf == 'quit') {
        break;
    }
    if ($buf == 'shutdown') {
        socket_close($msgsock);
        break 2;
    }
    $talkback = "PHP: You said '$buf'.\n";
    socket_write($msgsock, $talkback, strlen($talkback));
    echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);

socket_close($sock);
?>

When I run it on my browser with wamp it gives me an undefined function socket_create.

Tudor Luca
  • 6,259
  • 2
  • 29
  • 44

2 Answers2

0

Do you have php_sockets extensions enabled? You should try this.

<?php
if (!extension_loaded('sockets')) {
    die('The sockets extension is not loaded.');
}
?> 
Manoj Pandey
  • 4,528
  • 1
  • 17
  • 18
  • it says mysockets are not loaded so please how to i corrwectly load them cause i have tried editing the php.ini file i.e removing the ";" from the ";extension=php_sockets.dll" –  Sep 07 '13 at 22:28
  • Here is a good discussion on how to enable the socket module in PHP: http://stackoverflow.com/questions/1361925/how-to-enable-socket-in-php . Please try those steps. – Manoj Pandey Sep 07 '13 at 22:43
0

You have to install socket extention

http://www.php.net/manual/en/sockets.installation.php

And see the following post https://stackoverflow.com/a/6137855/716865

Community
  • 1
  • 1
Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46