Jump to content

[SOLVED] php socket_server. Disconnecting client problem


zaez

Recommended Posts

Alright i'm running my server on my localip, accesible only for our network.

the loop:

private function listenToSockets()

{

 

while (true) {

 

$changed_sockets = $this->currentSockets;

 

$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);

 

foreach($changed_sockets as $socket)

{

if ($socket == $this->masterSocket)

{

if (($client = socket_accept($this->masterSocket)) < 0) {

continue;

}

else

{

 

// NEW CLIENT HAS CONNECTED

$this->currentSockets[] = $client;

socket_getpeername($client, $newClientAddress);

}

}

else

{

$bytes = socket_recv($socket, $buffer, 4096, 0);

if ($bytes == 0)

{

 

// CLIENT HAS DISCONNECTED

$index = array_search($socket, $this->currentSockets);

 

// Remove Socket from List

unset($this->currentSockets[$index]);

 

$this->game->removePlayer($socket);

 

socket_close($socket);

 

}

else

{

 

// CLIENT HAS SENT DATA

$this->parseRequest($socket, $buffer);

}

}

}

}

}

 

the client side is AS3, connecting through XMLSocket, and talking to the server using XML data.

 

Well everything works good. The problem is that if i disconnect the client from the network (unpluging the cable, or just disconnecting the wireless connection), this:

 

if ($bytes == 0)

{

 

// CLIENT HAS DISCONNECTED

$index = array_search($socket, $this->currentSockets);

 

// Remove Socket from List

unset($this->currentSockets[$index]);

 

$this->game->removePlayer($socket);

 

socket_close($socket);

 

}

won't trigger, as if the socket would be still sending data.

 

So, question:

How do i make my server script detect when a client disconnects from the network?

Any help, or advices welcome and appreciated.

Thank you for looking!

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.