tags:

views:

41

answers:

1

I'm debating on how to map a User struct to their socket file descriptor. I was considering keeping an array of void pointers of a MAX_CONNECTIONS size, and storing the pointer as the value for the key of the file descriptor.

The issue I run into is how I plan on handling my receives. I currently call pipe() to make a pipe, and then fork() the process to handle recv. I would likely want to keep track of these, too, to be able to handle.

What I currently do is loop through a linked list of users, find the one matching the descriptor, and go from there

Question: Is this an efficient and/or acceptable method of mapping sockets to the users connected to them, or should I practice some other sort of voodoo?

I appreciate any effort given toward my (likely pointless) dilemma.

+2  A: 

If you don't have lots of clients, then it's fine. You won't spend a lot of time for just a bunch of users. You may want to try using hashtable (or a dictionary, associative array).

There are countless free/open implementations in ANSI-C, or you can just roll your own. You just need some unique and constant identifier per User, which is going to be your key. If you want to have a bidirectional relationship (bijective map) you can just have another hashtable where you store the users by file descriptor.

Operations on a hashtable are O(1) average. The hashtable itself uses O(N) space, where N is a big number (but not too large). Don't worry about that either.

Take a look at this one. You may also be interested in this question.

Pablo Alejandro Costesich
I've been doing my own since this is all a learning exercise. Thank you for the resources and answer!
Codeacula
You're welcome!
Pablo Alejandro Costesich