Socket server in browser

Socket server in browser

am 02.08.2007 17:13:35 von bartosz.zaleski

Hi
I am running the PHP Socket Server:

#!/usr/bin/php -q

error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();

$address = '127.0.0.1';
$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);
}}}}
?>


I activate it with the *.bat file:
c:/php/php.exe -q c:/Apache/htdocs/socketServer.php


But I want to use it at the external server and there is no way to
activate it there (no access to command line).
My question: is it possible not to use such a server in the daemon-
mode but to activate it on-demand (by Flash client)?

Re: Socket server in browser

am 02.08.2007 23:59:15 von Hendri Kurniawan

On Aug 3, 1:13 am, bartosz.zale...@gmail.com wrote:
> Hi
> I am running the PHP Socket Server:
>
> #!/usr/bin/php -q
> >
> error_reporting(E_ALL);
> set_time_limit(0);
> ob_implicit_flush();
>
> $address = '127.0.0.1';
> $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);}}}}
>
> ?>
>
> I activate it with the *.bat file:
> c:/php/php.exe -q c:/Apache/htdocs/socketServer.php
>
> But I want to use it at the external server and there is no way to
> activate it there (no access to command line).
> My question: is it possible not to use such a server in the daemon-
> mode but to activate it on-demand (by Flash client)?

If I'm not mistaken you can... but let the other ppl from the
newsgroup clarify me.
You can do this by issuing the following command:
exec('start c:/php/php.exe -q c:/Apache/htdocs/socketServer.php');

But if you haven't got access to command line on the other server
(I'm assuming it's a web-host), I highly doubt you can *listen* to any
port
due to firewall restriction.

Hendri Kurniawan

Re: Socket server in browser

am 03.08.2007 03:59:56 von bartosz.zaleski

So is there no other way to establish socket-server than to run it on
my own server?

Re: Socket server in browser

am 03.08.2007 04:12:12 von Jerry Stuckle

bartosz.zaleski@gmail.com wrote:
> So is there no other way to establish socket-server than to run it on
> my own server?
>

Hendri is correct - if this is a shared server, chances are you won't be
allowed to run it.

Can you imagine what it would be like if 100 websites all tried to use
the same port?

You can get your own dedicated server or even a VPS. That way you have
(pretty much) complete control over the system.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Socket server in browser

am 03.08.2007 06:20:09 von bartosz.zaleski

Not good :/

Anyway thanks lads for info.