Jump to content

Sockets not working


bradweston

Recommended Posts

Basically I don't know why but this

if (in_array($this->Clients[$I]['sock'], $read))

doesn't pass where as it should the new connection doesn't go into the read array because even though that would fix it it just loops arround continuously.

 

 

Ok here is my code

<?php

class WebODSockServer {
    
    public $Sock = NULL;
    public $Port = NULL;
    public $totalConnections = 0;
    protected $isAuth = false;
    public $Clients = array();
    protected $Funcs = array();
    
    function __construct ($Port){
        set_time_limit(0);
        $this->Port = $Port;
    }
    
    function addProcess ($Command, $Func, $requiresAuth = false){
        $this->Funcs[$Command] = array($Func, $requiresAuth);
        return;
    }
    
    function init (){
        $this->Sock = socket_create(AF_INET, SOCK_STREAM, 0);
        //socket_bind($this->Sock, WEBOD_IP, $this->Port);
        socket_bind($this->Sock, '127.0.0.1', $this->Port);
        socket_listen($this->Sock);
        echo "WebOD Socket Server." . PHP_EOL;
        $this->totalConnections = 0;
        while(true){
            $read[0] = $this->Sock;
            
            foreach(array_keys($this->Clients) as $I){
                if(isset($this->Clients[$I]['sock']))
                    $read[] = $this->Clients[$I]['sock'];
            }
            
            if (socket_select($read, $write = NULL, $except = NULL, $tv_sec = 5) < 1)
                continue;
            
            if (in_array($this->Sock, $read)) {
                $this->totalConnections++;
                echo "New user." . PHP_EOL;
                $this->Clients[]['sock'] = socket_accept($this->Sock);
        	}
            
            foreach(array_keys($this->Clients) as $I){
                if(isset($this->Clients[$I]['sock'])){
                    if (in_array($this->Clients[$I]['sock'], $read)){
                        echo "I got this far.";
                        $input = socket_read($this->Clients[$I]['sock'], 1024);
                        if($input == NULL) $this->sayGoodbye($I);
                        elseif(strtolower($input) == 'quit'){
                            socket_write($this->Clients[$I]['sock'], 'Goodbye', strlen('Goodbye'));
                            $this->sayGoodbye($I);
                            continue;
                        }elseif(strtolower($input) == 'auth'){
                            echo "Auth Called." . PHP_EOL;
                            $this->Clients[$I]['webodauth'] = 1;
                            socket_write($this->Clients[$I]['sock'], 'Welcome to WebOD', strlen('Welcome to WebOD'));
                        }else{
                            $input = explode(' ', $input, 1);
                            if($Key = array_search($input[0], array_keys($this->Funcs))){
                                $Function = $this->Funcs[$Key];
                                if(is_array($Function)){
                                    $Function[0]->$Function[1]($input[1]);
                                }else{
                                    $Function($input[1]);
                                }
                            }else{
                                socket_write($this->Clients[$I]['sock'], 'Invlaid Command.', strlen('Invlaid Command.'));
                            }
                        }
                    }else $this->sayGoodbye($I);
                }
            }
            
        }
        socket_close($this->Sock);
        return;
    }
    
    function sayGoodbye ($ClientID){
        socket_close($this->Clients[$ClientID]['sock']);
        unset($this->Clients[$ClientID]);
        echo sprintf("User %d left.", $ClientID) . PHP_EOL;
    }
    
    function tryAuth (){
        
    }
    
}

$W = new WebODSockServer(9099);
$W->init();

 

Many thanks!

Link to comment
https://forums.phpfreaks.com/topic/241928-sockets-not-working/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.