Proper way to do this?

0___________0

Senior member
May 5, 2012
284
0
0
I have a simple C# server app, it handles connections from multiple clients. One thing I need to be able to do is select one client, and communicate with it. To accomplish this I have created a dictionary:
Code:
Dictionary<Socket, int> clients = new Dictionary<Socket, int>();
The server is asynchronous, and ever time the callback that accepts a new client is called it adds the socket and a unique ID to the dictionary. Once I have the specific socket I can read and write data from and to the client. The console app takes my input "client:x" and passes x to a method that uses LINQ to select the key(socket) from the database where the value equals x. In my case, the value is always unique, so that's not a problem. But as the dictionary grows, I'm checking every entry in it and just making a brute force check constantly. It also requires framework version 3.5. So I'm wondering what is the proper way to do this? My solution works, but I wouldn't call it ideal.
 
Last edited:

Merad

Platinum Member
May 31, 2010
2,586
19
81
I'm half asleep and may be misunderstanding something, but....

You input the Socket, and need to retrieve its ID, correct? It sounds like you want the TryGetValue method of Dictionary<>.

Code:
Dictionary<Socket, int> clients = new Dictionary<Socket, int>();
...

Socket s = /*whatever*/;
int id;
if (clients.TryGetValue(s, out id))
{
  // use the id
}
 

0___________0

Senior member
May 5, 2012
284
0
0
I need to retrieve the socket. The problems with my current method are that it requires version 3.5 and isn't suited to a large number of entries. Here's how I get the socket with LINQ:
Code:
clients.Where(y => y.Value == x).Select(y => y.Key);
"x" is passed to the method that runs the LINQ query; I store the resulting socket from the LINQ expression and use it to communicate with the client. This works, but I don't think its a very elegant solution. I'd like to be able to do it with a lower version of the framework and not have to brute force the dictionary by checking every single entry; I'm looking for an alternative to using the dictionary, a different approach. My current method works, I'm just looking for a better way.
 
Last edited:

Merad

Platinum Member
May 31, 2010
2,586
19
81
Then why aren't you using a Dictionary<int, Socket>?

I'm looking for an alternative to using the dictionary, a different approach.

Storing (key, value) pairs is precisely what a dictionary is intended for...
 

0___________0

Senior member
May 5, 2012
284
0
0
That works; but in an effort to expand my meager knowledge I was looking to achieve the same result by a different means.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
Well if you're worried about performance, I doubt you'll easily beat Dictionary, which is pretty fast since its implemented with a hash table. Certainly it would fall under premature optimization unless you've actually tested and identified this as a bottleneck.

If you just want to learn how to implement something like a dictionary I'd just do some reading on implementing a hash table or etc.
 

Train

Lifer
Jun 22, 2000
13,583
80
91
www.bing.com
Dictionary<int, Socket> would be my choice here. Generics were introduced in .Net 2.0 so you can go as far back as that. Just use the HasKey and TryGetValue methods instead of LINQ. LINQ is likely uneeded overhead in this case anyways.

If you want to use .Net 1.1, you could always make your own implementation of IDictionary, or if memory serves, wrap DictionaryBase to cast the keys to int and the value to Socket.