Gönderen Konu: Php Socket Server  (Okunma sayısı 18853 defa)

Php Socket Server

« : 24.03.2004 23:33:07 »
Hızlı düğmeleri aç

spaztica

İleti: 1.493

Çevrimdışı
  • Administrator
  • *****
  • Hero Member
    • Profili Görüntüle
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: [Seç]
#!/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], $msgstrlen($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
serverremove 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_INETSOCK_STREAM0);
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($listenfdSOL_SOCKETSO_REUSEADDR0); 
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;
}
}
}

?>


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

Kod: [Seç]
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+" ";

}
}

Php Socket Server

« Yanıtla #1 : 27.03.2007 11:55:42 »
Hızlı düğmeleri aç

atiflz

İleti: 246

Çevrimdışı
  • ***
  • Full Member
    • Profili Görüntüle
    • http://izleyenkisi.blogspot.com/
Güzel bir şeye benziyor, ilk fırsatta inceleyeceğim, sağolasın.

Php Socket Server

« Yanıtla #2 : 27.03.2007 12:26:28 »
Hızlı düğmeleri aç

jimqode

İleti: 215

Çevrimdışı
  • ***
  • Full Member
    • Profili Görüntüle
    • http://
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.
/*
Planet Retro - Retrocomputing News and Blogs
*/

Php Socket Server

« Yanıtla #3 : 27.03.2007 12:37:32 »
Hızlı düğmeleri aç

skate

İleti: 5.245

A Sinner Scener
Çevrimdışı
  • Administrator
  • *****
  • Hero Member
    • Profili Görüntüle
    • http://www.akaydin.com/
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.

Php Socket Server

« Yanıtla #4 : 27.03.2007 13:35:16 »
Hızlı düğmeleri aç

jimqode

İleti: 215

Çevrimdışı
  • ***
  • Full Member
    • Profili Görüntüle
    • http://
gui de mi var? ben hic kurcalamamistim. php-gtk ile yapiliyor gui sanirim ama kendi icine de koydularsa bilemiyorum.
/*
Planet Retro - Retrocomputing News and Blogs
*/

Php Socket Server

« Yanıtla #5 : 27.03.2007 14:15:04 »
Hızlı düğmeleri aç

skate

İleti: 5.245

A Sinner Scener
Çevrimdışı
  • Administrator
  • *****
  • Hero Member
    • Profili Görüntüle
    • http://www.akaydin.com/
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/

Php Socket Server

« Yanıtla #6 : 28.03.2007 16:14:34 »
Hızlı düğmeleri aç

puNky

İleti: 224

Çevrimdışı
  • ***
  • Full Member
    • Profili Görüntüle
oyy, ben görmeyeli neler olmuş :) php-gtk ha? sevindim bir bir php'ci olarak, ilk fırsatta deneyeceğim.

Php Socket Server

« Yanıtla #7 : 28.03.2007 22:32:46 »
Hızlı düğmeleri aç

skate

İleti: 5.245

A Sinner Scener
Çevrimdışı
  • Administrator
  • *****
  • Hero Member
    • Profili Görüntüle
    • http://www.akaydin.com/
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.

Php Socket Server

« Yanıtla #8 : 29.03.2007 00:30:40 »
Hızlı düğmeleri aç

jimqode

İleti: 215

Çevrimdışı
  • ***
  • Full Member
    • Profili Görüntüle
    • http://
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. :)
/*
Planet Retro - Retrocomputing News and Blogs
*/