C# Developers' Journal (original) (raw)
6:24p
Network Connection Heh... apologies for my last post, which I just now noticed got posted to this community, rather than to my own journal. At least it was vaguely on topic :)
I'm working on an application that is supposed to be able to handle (potentially) thousands of open TCP/IP connections I wrote a version in Java, but now it needs to be ported to C# (with which I'm a complete newbie), and I'm having trouble figuring out exactly how to do the socket handling.
I've got a server socket listening on a specified port. When a connection request is made, it accepts, and hands the new connection off to a ConnectionManager, who's sole job is to maintain a list of open connections, and send them off to a worker thread to be serviced when they're ready to be read from. I'm not exactly sure how best to go about determining when a socket is ready to be read from, though. The most straightforward route would seem to be to loop through my list of open sockets, and
Poll()
each one, but that's inefficient if the list is large. I could do an asynchronous
BeginRead()
on each one, but that seems inefficient too, since if I have 100 connections sitting idle, there will be 100 worker threads (or fewer, since the ThreadPool apparently defaults to 25 threads) sitting around doing nothing.
Socket.Select()
seems promising, since I was using selectors in the Java implementation, but
Select()
modifies the list that you pass into it. That means I can't just pass in my list of open connections, since any connections that aren't waiting to be read from will get tossed out. So I have to make a copy of that list to pass in every time I call
Select()
, which also seems pretty inefficient.
I'm sure I'm missing something, but I don't know what. Any suggestions?