5

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!

user1833028
  • 865
  • 1
  • 9
  • 18
  • Check out [`React`](https://github.com/reactphp/react), and consider using it as a complete library, or as a reference for what you're trying to do. I believe one of the included example scripts is a chatroulette style TCP chat server. – Leigh Dec 19 '12 at 09:53
  • Thanks... I might do that, but I can't understand if using this particular third party library adds any security risks. Perusing, it looks functionally great already. – user1833028 Dec 19 '12 at 09:56
  • The library is being quite widely used already (I use it myself), and there's a fair amount of community involvement in maintaining it. I believe there's probably more chance of there being a security risk in your homebrew code than in a publicly maintained library. – Leigh Dec 19 '12 at 10:00
  • Well, thanks! I'm new to PHP and I don't know much about the third party libraries. But if it has a wide support base, it'll probably do for my purposes. ... Though I'd still be interested in knowing how to check the sockets directly or with pure PHP.. not that I'm devaluing your suggestion, at all. Thanks again. – user1833028 Dec 19 '12 at 10:10
  • The library *IS* pure PHP. Everything you need is there. – Leigh Dec 19 '12 at 10:16
  • Oh, no, thank you. I meant to say WITHOUT a library, just as an exercise. Interesting.. I appreciate the clarification though. – user1833028 Dec 19 '12 at 10:29

1 Answers1

1

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

Community
  • 1
  • 1
Daniel
  • 3,726
  • 4
  • 26
  • 49
  • Sir, socket_read applies to the NON-stream version of sockets in PHP (I think, not sure, I'll try it again.) Try looking at: http://christophh.net/2012/07/24/php-socket-programming/. Agree with him or not, his basic description is informative. I don't think I can do something like that because in my case, a false return from, say a similar stream function would simply mean the server did not receive a message from that client prior to that iteration of the loop. – user1833028 Dec 19 '12 at 10:14
  • I'm not sure but wouldn't [socket_import_stream](http://us2.php.net/manual/en/function.socket-import-stream.php) allow you to use this method? I might be off though. – Daniel Dec 19 '12 at 10:17
  • Interesting.. what IS a socket extension resource? Again, I can't use a false return on a read function, because they will return false as long as there is no new message from that client, even if they are still connected. Or should I have built it so that does NOT happen? – user1833028 Dec 19 '12 at 10:20
  • AFAIK a socket resource is only a type-definition for the socket functions. Since the socket_read only will return false if the remote host has disconnected, and an empty string if no message has been recieved - I thought you might be able to use this. `Edit` My bad, the function will return false in any case. Maybe you can check with the `socket_last_error`, but i guess it won't be the best alternative. – Daniel Dec 19 '12 at 10:24
  • I certainly appreacite the thought, but socket_last_error returns "success." It was actually kinda funny: my server terminal was spewing ERROR:SUCCESS among other things every 1 second (unless there was some other problem I was never aware of.) – user1833028 Dec 19 '12 at 10:40