tr-demoscene
the scene => scripting => php => Konuyu başlatan: spaztica - 24.03.2004 23:33:07
-
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.
#!/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 = "Daemon going down!
";
for ($i = 0; $i < FD_SETSIZE; $i++)
{
if ($client[$i] != null)
{
socket_write($client[$i], $msg, strlen($msg));
socket_close($client[$i]);
}
}
print "Shutting down the daemon
";
socket_close($listenfd);
exit;
}
// whenever a client disconnects...
function closeClient($i)
{
global $client, $remote_host, $remote_port;
print "closing client[$i] ({$remote_host[$i]}:{$remote_port[$i]})
";
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 "Listening on port " . PORT . "
";
else
die("AIEE -- socket died!
");
//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, "0.0.0.0", PORT))
{
socket_close($listenfd);
die("AIEE -- Couldn't bind!
");
}
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 "listenfd heard something, setting up new client
";
//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 "Accepted {$remote_host[$i]}:{$remote_port[$i]} as client[$i]
";
// continue the server if connection allowed else allow next step
if ($i < FD_SETSIZE)
{
break;
}
}
if ($i >= FD_SETSIZE)
{
//trigger_error("too many clients", 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 == "/killme")
killDaemon();
else if ($n == "/quit")
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 "From {$remote_host[$i]}:{$remote_port[$i]},>client[$i]: $n
";
for ($j = 0; $j <= FD_SETSIZE; $j++)
{
if ($client[$j] != null)
socket_write($client[$j], "From client[$i]: $n".chr(0));
//the chr(0) send the nul character required by flash
}
}
}
if (--$nready <= 0)
break;
}
}
}
?>
bu da Flash'dan sockservera teorik bağlantı yapısı:
xml.fla (http://www.obsesif.info/files/xml.fla) adresinde de çalışan bir örnek bulunuyor. B)
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+" ";
}
}
-
Güzel bir şeye benziyor, ilk fırsatta inceleyeceğim, sağolasın.
-
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.
-
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.
-
gui de mi var? ben hic kurcalamamistim. php-gtk ile yapiliyor gui sanirim ama kendi icine de koydularsa bilemiyorum.
-
php-gtk unoffical bir extension değil şurdan da anlaşılacağı üzere;
Official PHP web site:
http://www.php.net/
Official PHP-GTK web site:
http://gtk.php.net/
-
oyy, ben görmeyeli neler olmuş :) php-gtk ha? sevindim bir bir php'ci olarak, ilk fırsatta deneyeceğim.
-
PHP süper bir dil ancak bu topic'in açılış nedeni olan PHP-Flash ilişkisi de çok hoş aslında. PHP'den XML'i oluştur, Flash ile göster v.s. çok güzel kullanım alternatifleri sunuyor bu ikili. Zaman içersinde daha çok Flash kullandıkça Spaztica'nın bilgi birikiminden yararlanmaya başlayacağım bu konuda. Çok uzun zamandır Spaztica ikiliyi Flash ağırlıklı olarak kullanıyor çünkü.
Bu arada ne yazık ki Action Script hala hız olarak bazı konularda geri kalıyor. Testlerimin sonucunda yapısal birçok benzerlik taşıdığı JavaScript'den hızlı ancak diğer birçok scriptingden yavaş olduğunu gördüm. Hatta hatta vbs (Visual Basic Script) bile recursive fonksiyonlarda Action Script'i geçiyor. Sebebi hakkında ise hiçbir fikrim yok. Ancak ben hala Flash MX 2004 kullanıyorum. Belki Flash 9'da daha hızlanmış olabilir Action Script ki böyle birşeyler okumuştum. Belki Spaztica bu konuda da bizi aydınlatabilir.
-
Flashi ben yapisal olarak begenmiyorum acikcasi. Daha once cok flash is yaptim. Bugun o islerin sourcelarini (hos source diye bisey yok fla iste) acik baktigimda bi kodu bulmaya calisirken kayboluyorum. Kodlari nesnelerle iliskilendirmek basta iyi bir fikir gibi gozukse de sagda soldaki nesnelerde dagilmis bulunan kod parcalarini edit etmeye gelince is guzelligini yitiriyor. Bunun icin .as include etmek gibi trickler varolsa da olayin mantigi code reuse'u azaltiyor ve bazi seyleri yapmak icin kulagi ters taraftan gostermek gerekiyor.
Ama skate'in de dedigi gibi ActionScript 3'te bircok sey degismis olabilir. Ben de en son MX2004 kullanmis ve son projemden kufur kafir seklinde kurtulmustum. :)