1

I'm creating a multithreaded server, and i've stucked on this problem. To accept and handle connections i'm using socket_accept, and then creating a Connection object, adding it to array, and then fetching through all array. But for some reason, when i'm doing $connection::read (which does socket_read) the socket in this objects becomes 0. But when object is constructed, everything is fine, dumping socket returns resource, but dumping the same socket in read() throws warnings, because socket is 0. Here some code

ThreadedServer, run()
        while ($this->enabled){
            foreach ($this->connections as $connection){
                /** @var $connection Connection */
                $msg = $connection->read(); //here it's already 0, and i'm getting many warnings
                if($msg){
                    //todo
                }
            }
            if (($clientSocket = socket_accept($this->socket))) {
                socket_getpeername($clientSocket, $addr, $port);
                $this->connections[$hash = self::hash($addr, $port)] = new Connection($clientSocket, $addr, $port, $this);
                $this->logger->info("New connection accepted: $hash");
            }
        }
Connection, in same thread with the ThreadedServer
    public function __construct($socket, string $address, int $port, ThreadedServer $server)
    {
        $this->socket = $socket;
        //socket_set_nonblock($this->socket); //not needed?
        var_dump($this->socket); //here it returns 'resource'
        $this->server = $server;
        $this->lastUpdate = microtime(true);
        $this->address = $address;
        $this->port = $port;
    }

    public function read(){
        var_dump($this->socket); //here it returns 'int (0)'
        return socket_read($this->socket, 1024);
    }


Yexeed
  • 33
  • 3
  • BUT, if i will add the socket itself to array (like `$arr[$hash] = $socket;`) then everything will be fine – Yexeed Feb 05 '19 at 19:00
  • "The socket resource returned by socket_accept() may not be used to accept new connections. The original listening socket socket, however, remains open and may be reused." – ficuscr Feb 05 '19 at 19:13
  • @ficuscr, so? I'm aint accepting new connections through the clientSocket, only using serverSocket (which in ThreadedServer). I'm just moved the clientSocket in the Connection object, so why clientSocket becomes 0 just before the object was created? `$this->connections[$hash] = new Connection($socket, ...); //dumping socket in connection constructor returns 'resource of type stream'` `var_dump($this->connections[$hash]->getClientSocket()); //returns 'int (0)'` – Yexeed Feb 05 '19 at 19:20
  • Sorry. Having a hard time following the code provided. Can't tell if you overwrite the thing between dumping resource and dumping int 0 (in read). – ficuscr Feb 05 '19 at 19:35
  • @ficuscr, can this problem being created by making field/property $connections volatile, and why is it? And how can I solve it? – Yexeed Feb 05 '19 at 19:50
  • [$connections dump](https://imgur.com/a/FmYv4wd) – Yexeed Feb 05 '19 at 19:52
  • Again, I can't follow your code. Do you ever even do a `socket_create`? Can't help you with what has been provided. – ficuscr Feb 05 '19 at 19:55
  • @ficuscr, ok, take a look at this https://hastebin.com/vemogeciwa.php – Yexeed Feb 05 '19 at 20:00

0 Answers0