Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Technique to address sockets stored in an array

1 view
Skip to first unread message

benjamin.s...@googlemail.com

unread,
Nov 2, 2009, 4:11:24 PM11/2/09
to
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!

Benjamin

Jerry Stuckle

unread,
Nov 2, 2009, 8:36:18 PM11/2/09
to

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.
jstu...@attglobal.net
==================

C. (http://symcbean.blogspot.com/)

unread,
Nov 3, 2009, 5:32:42 AM11/3/09
to
On Nov 2, 9:11 pm, "benjamin.schneider...@googlemail.com"

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

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.

C.

Danny Wilkerson

unread,
Nov 6, 2009, 7:36:41 AM11/6/09
to
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 :(

0 new messages