PDA

View Full Version : Need Help



asiridol
November 30th, 2006, 05:26 AM
I tried the following coding.. & I got a error which says socekt_creation is a unknown method.. what should I do?:sigh:

#!/usr/bin/php -q <?php /* Raymond Fain Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com For any questions or concerns, email me at ray@obi-graphics.com or simply visit the site, www.php.net, to see if you can find an answer. */ error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); $address = '192.168.0.16'; $port = 9999; //---- Function to Send out Messages to Everyone Connected ---------------------------------------- function send_Message($allclient, $socket, $buf) { foreach($allclient as $client) { socket_write($client, "$socket wrote: $buf");}} //---- Start Socket creation for PHP 5 Socket Server ------------------------------------- if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { echo "socket_create() failed, reason: " . socket_strerror($master) . "\n";} socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1); if (($ret = socket_bind($master, $address, $port)) < 0) { echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\n";} if (($ret = socket_listen($master, 5)) < 0) { echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\n";} $read_sockets = array($master); //---- Create Persistent Loop to continuously handle incoming socket messages --------------------- while (true) { $changed_sockets = $read_sockets; $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL); foreach($changed_sockets as $socket) { if ($socket == $master) { if (($client = socket_accept($master)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n"; continue;} else { array_push($read_sockets, $client);}} else { $bytes = socket_recv($socket, $buffer, 2048, 0); if ($bytes == 0) { $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_close($socket);}else{ $allclients = $read_sockets; array_shift($allclients); send_Message($allclients, $socket, $buffer);}} }} ?>

bwh2
November 30th, 2006, 09:39 AM
right. my advice is to put that code block
here's the code tags and to split it up line by line like normal code. and you should probably remove that email address.

Danii
November 30th, 2006, 07:17 PM
apperantly you dont have sockets enabled in your php module.. i know on unix you have to configure with --enable-sockets to be able to use the socket functions.

EDIT: If your running windows uncomment ;extension=php_sockets.dll in your php.ini file.

Hope it helps

Seb Hughes
December 1st, 2006, 10:35 AM
Erm as Bwh2 said, put the code in PHP tags or we cant help you, as we dont want togo though the code in that state.

asiridol
December 4th, 2006, 10:02 PM
Sorry guys for the inconvenience… Here is what I came across with all the description. Hope you people will help me..

This is the error I am getting "Fatal error: Call to undefined function socket_create() in ~path~\socketTut.php on line 46"

This is the code

#!/usr/bin/php -q
<?php
error_reporting(E_ALL);

set_time_limit(0);

ob_implicit_flush();

1024 so flash will connect
$address = '192.168.0.16';
$port = 9999;

function send_Message($allclient, $socket, $buf) {
foreach($allclient as $client)
{
socket_write($client, "$socket wrote: $buf");
}
}

if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0)
{
echo "socket_create() failed, reason: " . socket_strerror($master) . "\n";
}

socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);

if (($ret = socket_bind($master, $address, $port)) < 0)
{
echo "socket_bind() failed, reason: " . socket_strerror($ret) . "\n";
}

if (($ret = socket_listen($master, 5)) < 0)
{
echo "socket_listen() failed, reason: " . socket_strerror($ret) . "\n";
}


$read_sockets = array($master);

while (true)
{
$changed_sockets = $read_sockets;

$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);

foreach($changed_sockets as $socket)
{
if ($socket == $master)
{
if (($client = socket_accept($master)) < 0)
{
echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
continue;
}
else
{
array_push($read_sockets, $client);
}
}
else
{
$bytes = socket_recv($socket, $buffer, 2048, 0);
if ($bytes == 0)
{
$index = array_search($socket, $read_sockets);
unset($read_sockets[$index]);
socket_close($socket);
}
else
{
$allclients = $read_sockets;
array_shift($allclients);

send_Message($allclients, $socket, $buffer);
}
}
}
}

?>

bwh2
December 4th, 2006, 10:13 PM
what version of PHP are you running. to test, create a new php doc and put this in there:


<?php
phpinfo();
?>

nobody
December 4th, 2006, 10:13 PM
I put your code into php tags which was already suggested to you. Please click edit on your post and look at how it was done so you can do it next time. :)

asiridol
December 4th, 2006, 10:28 PM
what version of PHP are you running. to test, create a new php doc and put this in there:


<?php
phpinfo();
?>


PHP 5.0

asiridol
December 4th, 2006, 10:29 PM
I put your code into php tags which was already suggested to you. Please click edit on your post and look at how it was done so you can do it next time. :)

thnx bro... sorry for the trouble I made..:shifty:

asiridol
December 7th, 2006, 10:08 PM
:crying: