Socket Collection

P

Paul W

I have a socket-based app running that spawns a different socket for each
user and I was looking for a way to optimize it a little better.

Currently I keep a collection of active sockets (300 - 400). My plan was to
capture the Socket IntPtr Handle when they connect and then each time I
needed to send information to that particular socket, I would call the
private Socket constructor via Reflection and use the IntPtr value as the
handle parameter. I would be maintaining a collection of 32 bit values
rather than a collection of Sockets.

My question is... do I gain anything by doing this? (1) Is reflection fast
enough in this instance. (2) Do I really get any type of performance gain
by storing the handle and not the object?

Thanks.
 
P

Paul W

Nevermind... this way loses the reference to the socket, so it is not a
good alternative.
 
W

William Stacey [MVP]

I would not think so. Just keep an arraylist or other collection of
sockets. You want the socket objects to live for life of connection.
 
F

feroze

I am not sure you gain anything here. For starters, if I am getting this
correctly, you want to do the following:

Socket client = serversocket.Accept();
myhandlelist.Add(client.Handle);

and then you will use the IntPtr from then on.

First notice, that you are storing the native handle. That means the OS
resources backing the socket have already been allocated. So, the most you
are saving is managed memory on the GC.

You might want to rethink the architecture of your application to make sure
you are using resources efficiently. For example, if your connections are
longlived (session oriented) then you might want to keep the connections
around for a certain amount of time. This involves coming up with a
connection management algorithm, whereby you time out and close idle
connections etc. However you will have to weigh the potential gain in
performance with respexct to the efffort involved in
designing/implementing/debugging such a scheme.

--

Thanks

feroze
=============
This posting is offered as-is. It offers no warranties and confers no
rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top