Re: Technique to address sockets stored in an array wrote:
Dear all,

I have a tiny question concerning the following problem:
I am currently writing a chat server in PHP. In this application I
have a class called Client which contains the socket resource for this
client. The Client objects are then stored in an array.
Now I want to know what is the most efficient way to find a socket
(e.g. for deleting it when socket_select() tells me that one
disconnected or to send a message to it).
For now I iterate through all the Client objects and compare the
socket resources. I thought of using the socket resources as the array
key but unfortunately this does not seem to work (when I want to use
the key as a socket resource in a socket function, the function aborts
with an "is not a resoure" error message).

So my next thought was it to retrieve the IP address and port of the
client and use this string as the array key. But I am not sure how
good the performance of the socket_getpeername() function is.

So maybe one of you can help me on this topic?

Thanks in advance!


You didn't post any code, so it's impossible to provide any intelligent help.

socket_getpeername() is going to be slow since it queries the remote. If the remote is not responding (vs. disconnected), it will wait until the request times out.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Relevant Pages