sock example

  Nokia

Scriptname :
Includes
socke.inc

by Toppi ( http://kacke.de/php_samples/source.php?f=socke.inc  http://kacke.de/php_samples/source.php?f=pline.php)
<?
/*============================================================================*/
|
|   Little Partyline example based on class socke()
|
|      by Toppi
|
|   forgett my poor english, if you can !
|   and please ....
|    if you like my work, respect me and dont remove my Notes :-)
|
|      2004
|
|
|
|  Copyright (C) 2001-2004 by Toppi (toppi@kacke.de)
|
|  This program is free software
|  you can redistribute it and/or modify
|  it under the terms of the GNU General Public License as published by
|  the Free Software Foundation; either version 2, or (at your option)
|  any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
/*============================================================================*/

error_reporting (7);
set_time_limit (0);
ob_implicit_flush ();

/*============================================================================*/
                                    Create Socket
/*============================================================================*/

require ("socke.inc");

if(
$socket = new socke()){
    
$io    $socket->listen("",23456);
}else {
    echo 
"Open socket failed! Dying.../n";
    exit;
}

/*============================================================================*/
                                    MAIN LOOP
/*============================================================================*/

echo "<b>PLINE SERVER RUNNING at:<br>";
echo 
"<b>".$socket->myhost.":".$socket->myport."</b><br>";
echo 
"telnet this :)<br>";

while(
1){
    if (
connection_status()!=0){
        
$socket->close();
        die;
    }
     Echo 
"/n";
    
usleep(200);
    foreach(
$socket->can_read() as $sock){
        if(
$io == $sock){
            if(
$mysock $socket->accept()){
                
//Neue Connex
                
login($mysock);
            }
        } else {
            
//Anyone had something to say
            
$data $socket->read($sock);
            
//Client pinged out
            
if($data === false){
                
logout($sock,"Connection reset by Beer");
                continue;
            }
            
//Only Enter Hit
            
if(!$data || $data == "/n" || $data == "/r/n"){
                continue;
              }
            
interact($sock$data);
        }
    }
}
/*============================================================================*/
                                    Functions
/*============================================================================*/

function interact ($mysock$data){

    GLOBAL 
$socket;

    
$cmd    explode(" ",$data);

    switch(
strtoupper($cmd[0])){

        case 
"/QUIT":
            
logout($mysock);
            return;
        case 
"/NICK":
            
set_nickname($mysock,$cmd[1]);
            return;
          case 
"/WHOIS":
            
whois($mysock,$cmd[1]);
            return;
          case 
"/WHO":
            
who($mysock);
            return;
        case 
"/SHUTDOWN":
            
shutdown();
            break;
    }

    if(!
$socket->get_socketinfo($mysock,"nick")){
        
write_me($mysock,"*** Enter your Nickanem first !");
        return;
    }
    
write_all($mysock,"$data");

    return;
}

function 
whois($mysock$nick=""){

    GLOBAL 
$socket;

    if(!
$nick){
        
write_me($mysock,"You need help !Usage: whois <nick>!");
        return;
    }
    
$sockinfos $socket->get_socketinfo();
    
$nick strtoupper($nick);
    foreach(
$sockinfos as $sock){
        if(
strtoupper($sock['nick']) == $nick){
            
write_me($mysock,"Whois: /"$nick/"");
            
write_me($mysock,"Host: /"$sock[host]/"");
            
write_me($mysock,"IP: /"$sock[ip]/"");
            
write_me($mysock,"Joined: /"".date("Y-m-d H:i:s",$sock['started'])."/"");
            
$last  time()-$sock['last'];
            
write_me($mysock,"Last spoke: /"$last/" seconds ago");
            return;
        }
    }
    
write_me($mysock,"Nick /"$nick/" not here !");
    return;
}

function 
who($mysock){

    GLOBAL 
$socket;

    
write_me($mysock,"Who is here ?");
    
$sockinfos $socket->get_socketinfo();
    foreach(
$sockinfos as $sock){
        
write_me($mysock," $sock[nick]");
    }
    return;
}

function 
set_nickname($mysock$nick=""){

    GLOBAL 
$socket;

    if(!
$nick){
        
write_me($mysock,"Nick /"$nick/" not valid !");
        return;
    }
    
$sockinfos $socket->get_socketinfo();
    
$nick1 strtoupper($nick);
    foreach(
$sockinfos as $sock){
        if(
strtoupper($sock['nick']) == $nick1){
            
write_me($mysock,"Nick /"$nick/" already in use !");
            return;
        }
    }
    
$socket->set_socketinfo($mysock,"nick",$nick);
    
write_me($mysock,"Your now known as /"$nick/"!");
    return;
}

function 
write_all($mysock$message){
    
//
    
GLOBAL $socket;

    if(
is_resource($mysock)){
        
$nick $socket->get_socketinfo($mysock,"nick");
    }else{
        
$nick $mysock;
    }
    foreach(
$socket->clients as $client){
        
$socket->write ($clientdate("H:i")."<$nick> $message/n");
    }
    return;
}

function 
write_all_but_me($mysock$message){

    GLOBAL 
$socket;

    
$nick $socket->get_socketinfo($mysock,"nick");

    foreach(
$socket->clients as $client){
        if(
$client != $mysock){
            
$socket->write ($clientdate("H:i")."<$nick> $message/n");
        }
    }
    return;
}

function 
write_me($mysock$message){
    
//
    
GLOBAL $socket;

    
$socket->write ($mysockdate("H:i")." $message/n");

    return;
}

function 
login ($mysock){

    GLOBAL 
$socket;

    
write_me($mysock"Hello Stupid ! Welcome to PLINE/n");
    
write_me($mysock"Commands beginning with /");
    
write_me($mysock"I know: /quit, /nick, /shutdown, /who, /whois /n");
    
write_me($mysock"Enter your Nickname !");
     return;
}

function 
logout ($sock$message "" ){
    GLOBAL 
$socket;

    
write_all_but_me($sock,"*** Left the partyline");

    
$socket->close($sock);

    return;
}

function 
shutdown ($message "Server goes shutdown now" ){

    GLOBAL 
$socket;

    echo 
"$message<br>";

    
write_all("SYSTEM""*** $message");
    
$socket->close();
    exit;
}
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值