Technique to address sockets stored in an array

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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!


Re: Technique to address sockets stored in an array wrote:
Quoted text here. Click to load it

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

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.

Re: Technique to address sockets stored in an array

On Nov 2, 9:11=A0pm, ""
Quoted text here. Click to load it

Why not just index the array by the resource id (casted to an

I'm not sure how well sparse arrays are managed by PHP - the garbage
collector will still be able to clean up the objects themselves but I
don't know if it can compact the array holding the references. It
might be healthier in terms of memory management (at the cost of some
CPU) to store the clients in a SPLDoublyLinkedList and scan through it
after socket_select returns to match the resource.


Re: Technique to address sockets stored in an array

it's been a while. A long time ago i was worked on a text MUD. Think i
would cycle through the sockets using select. I don't remember that
well but i did first try to read from the socket. I think that, either
you get null when you select or try to read but however i got it, once
i found the null i would remove the entry from my associative array. I
think the reason why i used an associative array was because i did not
want to have to rearrange the array after every disconnect. So
basicaly i had an array holding references that i could delete with
the socket as the connect/disconnect and a second array holding user
info that hung around for a short time so i could send out stored up
messages and do character interactions until they get back.

I wish my folders of unfinnished MUD projects were still useful :(

Site Timeline