the scene > php

Php Socket Server

(1/2) > >>

spaztica:
Flash'da socket server kullananlar için PHP'de simple sockserver; PHP 4.1+ olmalı ve SafeMode kapalı olmalı. Script timeout'u da 0 yaparak script'in 30 saniyede timeout olmamasına dikkat edin. Script bir client'ın yazdığı herşeyi diğer client'lara echo eder; /quit ise server'ı sonlandırır.


--- Kod: ---#!/usr/local/bin/php -q
<?php
//setting this to 0 lets scriphang around for ever
set_time_limit(0);



// defaults...

define('MAXLINE', 1024); // how much to read from a socket at a time
define('LISTENQ', 5); // listening queue
define('PORT', 10000); // the default port to run on
define('FD_SETSIZE', 2); // file descriptor set size (max number of concurrent clients)...


// for kill the server

function killDaemon()
{
global $listenfd, $client;
$msg = &quot;Daemon going down!
&quot;;
for ($i = 0; $i < FD_SETSIZE; $i++)
{
if ($client[$i] != null)
{
socket_write($client[$i], $msg, strlen($msg));
socket_close($client[$i]);
}


}
print &quot;Shutting down the daemon
&quot;;
socket_close($listenfd);
exit;
}


// whenever a client disconnects...

function closeClient($i)
{
global $client, $remote_host, $remote_port;

print &quot;closing client[$i] ({$remote_host[$i]}:{$remote_port[$i]})
&quot;;

socket_close($client[$i]);
$client[$i] = null;
unset($remote_host[$i]);


//check to see if anyone is still attached, this will shutdown the 
//remove down from here if you want to remove this option
server, remove if unwanted
$someoneconnected = false;
for ($i = 0; $i <= FD_SETSIZE; $i++){
if($client[$i] != null){
$someoneconnected = true;
break;
}
}

if($someoneconnected == false){
killDaemon();
}
//stop removing here, for the auto shutdown feature

}


// set up the file descriptors and sockets...

// $listenfd only listens for a connection, it doesn't handle anything
// but initial connections, after which the $client array takes over...

$listenfd = socket_create(AF_INET, SOCK_STREAM, 0);
if ($listenfd)
print &quot;Listening on port &quot; . PORT . &quot;
&quot;;
else
die(&quot;AIEE -- socket died!
&quot;);

//set servers ip here, leave if using a local host
//socket_setopt($listenfd, SOL_SOCKET, SO_REUSEADDR, 1); - old

socket_setopt($listenfd, SOL_SOCKET, SO_REUSEADDR, 0); 
if (!socket_bind($listenfd, &quot;0.0.0.0&quot;, PORT))
{
socket_close($listenfd);
die(&quot;AIEE -- Couldn't bind!
&quot;);
}
socket_listen($listenfd, LISTENQ);


// set up our clients. After listenfd receives a connection,
// the connection is handed off to a $client[]. $maxi is the
// set to the highest client being used, which is somewhat
// unnecessary, but it saves us from checking each and every client
// if only, say, the first two are being used.


//for ($i = 0; $i < FD_SETSIZE; $i++) - old

//$client[$i] = null;

//allow spaces for extra users, that will be automatically closed
for ($i = 0; $i < LISTENQ; $i++)
$client[$i] = null;



// the main loop.

while(1)
{
$rfds[0] = $listenfd;

//for ($i = 0; $i < FD_SETSIZE; $i++) - old
{
for ($i = 0; $i <= FD_SETSIZE; $i++)
if ($client[$i] != null)
$rfds[$i + 1] = $client[$i];
}


// block indefinitely until we receive a connection...

$nready = socket_select($rfds, $null, $null, null);


// if we have a new connection, stick it in the $client array,

if (in_array($listenfd, $rfds))
{
print &quot;listenfd heard something, setting up new client
&quot;;

//for ($i = 0; $i < FD_SETSIZE; $i++) - old
for ($i = 0; $i <= FD_SETSIZE; $i++) //pick up the new spot
{
if ($client[$i] == null)
{
$client[$i] = socket_accept($listenfd);
//socket_setopt($client[$i], SOL_SOCKET, SO_REUSEADDR, 1); - old

socket_setopt($client[$i], SOL_SOCKET, SO_REUSEADDR, 0);
socket_getpeername($client[$i], $remote_host[$i], $remote_port[$i]);
print &quot;Accepted {$remote_host[$i]}:{$remote_port[$i]} as client[$i]
&quot;;

// continue the server if connection allowed else allow next step
if ($i < FD_SETSIZE)
{
break;
}

}

if ($i >= FD_SETSIZE)
{
//trigger_error(&quot;too many clients&quot;, E_USER_ERROR);
//exit;
//or close the new socket, 
closeClient($i);
break;// continue the server
}

}

if (--$nready <= 0)
continue;
}


// check the clients for incoming data.

for ($i = 0; $i <= FD_SETSIZE; $i++)
{
if ($client[$i] == null)
continue;

if (in_array($client[$i], $rfds))
{
$n = trim(socket_read($client[$i], MAXLINE));

if (!$n)
closeClient($i);
else
{
// if a client has sent some data, do one of these:

if ($n == &quot;/killme&quot;)
killDaemon();
else if ($n == &quot;/quit&quot;)
closeClient($i);
else
{
// print something on the server, then echo the incoming
// data to all of the clients in the $client array.
//$n is incoming data

print &quot;From {$remote_host[$i]}:{$remote_port[$i]},>client[$i]: $n
&quot;;
for ($j = 0; $j <= FD_SETSIZE; $j++)
{
if ($client[$j] != null)
socket_write($client[$j], &quot;From client[$i]: $n&quot;.chr(0));
//the chr(0) send the nul character required by flash
}
}
}

if (--$nready <= 0)
break;
}
}
}

?>

--- Kod sonu ---

bu da Flash'dan sockservera teorik bağlantı yapısı:
xml.fla adresinde de çalışan bir örnek bulunuyor.  B)


--- Kod: ---on (release) {
_root.mysocket = new XMLSocket();


_root.mysocket.connect(_root.ip, _root.port);
//makes connections

_root.mysocket.onConnect = function(success) {
//on a result
if (success) {- if succedded
_root.status = "Connected";
} else {
_root.status = "Failed";
}
};

_root.mysocket.onClose = function(success) {
//when the connection is closed or lost
_root.status = "Closed";
};


_root.mysocket.onXML = xmlhandler;
//when data is received run this function
function xmlhandler(doc) {
_root.status = "data Recieved";
loadedvars = String(doc);
_root.datain+=loadedvars+" ";

}
}

--- Kod sonu ---

atiflz:
Güzel bir şeye benziyor, ilk fırsatta inceleyeceğim, sağolasın.

jimqode:
php sonunda gelise gelise all-purpose bir scripting dili oldu cikti. Herhalde bundan en cok memnun olanlardan biri de ben oldum :). Neredeyse komple uygulama gelistiricez artik python gibi.

skate:
jimqode: Niye neredeyse? PHP 5.x serisinin Python'a göre eksik bir tarafı mı var ki? GUI falan da var artık, herşey var.

jimqode:
gui de mi var? ben hic kurcalamamistim. php-gtk ile yapiliyor gui sanirim ama kendi icine de koydularsa bilemiyorum.

Navigasyon

[0] Mesajlar

[#] Sonraki Sayfa

Tam sürüme git