6

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!

6
  • Check out 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. Commented Dec 19, 2012 at 9: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. Commented Dec 19, 2012 at 9: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. Commented Dec 19, 2012 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. Commented Dec 19, 2012 at 10:10
  • The library IS pure PHP. Everything you need is there. Commented Dec 19, 2012 at 10:16

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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: 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.
I'm not sure but wouldn't socket_import_stream allow you to use this method? I might be off though.
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?
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.
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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.