Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: stream_socket_server()

Tags:

php

php-socket

I have a homebrew PHP TCP chat server, but it doesn't have a way to detect remote disconnects.

I would be grateful if someone knew a way to just take a stream_socket_server() and spit out everything connected to it.

Then you could run a loop like this, in psuedocode:

$main_socket=stream_socket_server("tcp://",....)

//Do Something.... say, wait for a connection (with stream_socket_accept())?

for (each CONNECTION in $main_socket)
{
//Do something with or to that connection
}

//Loop back... if you need to say, wait for another connection

Alternatively, could I check if a variable created with $stream_socket_accept() is presently connected.

This project is bust until I figure this out. I'd be grateful to anyone who could help me out on this one!

like image 347
user1833028 Avatar asked Nov 12 '22 14:11

user1833028


1 Answers

The correct way of knowing if the remote host has disconnected is to test for a false socket_read(), as far as I know.

Have a look at this question; PHP - Detecting remote host disconnection

like image 77
Daniel Avatar answered Nov 15 '22 05:11

Daniel