Link to home
Start Free TrialLog in
Avatar of oleber
oleberFlag for Portugal

asked on

Having 10000 TCP/IP connections open at some time

Hi all

I need to have a server managing allot of connection in simultaneous. Having a thread for each connection is out of question.

So how do I do this in just one Thread managing all the connections?

Something like the POSIX select command
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Even two connections on the same thread is too many, since any problem in just one of the connections can crash or block them all. Any serious application will therefore use one thread per connection
Avatar of oleber

ASKER

Asycronous IO in java ? ? ?
In my experience, if you want something like select, you'll have to write it yourself.  Have a master thread controlling incoming connections and a multiplexing controller that polls connectiong for read/write ready states.

You might find this page useful: http://www.developer.com/net/cplus/article.php/626271
Avatar of oleber

ASKER

That was my first idea, but is there a better solution, not forcing me to have a sleep in the code?

>>Asycronous IO in java ? ? ?

Not sure what you mean by that...
You don't have to sleep with the multiplexor concept, you can ahve the multiplexor constantly going through a list of connections polling them.

You can have a separate pair of threads for read and write respectively.  NB: this requires some shared memory vodoo!

e.g.

// multiplexor
for(int i = 0; i < connections.length; i++){
  //poll connection[i] for read
  readyToRead[i] = true;
  //poll connection[i] for write
  readyToWrite[i] = true;
}

//read loop
for(int i = 0; i < connections.length; i++){
  if (readyToRead[i])
  // do read action
}

//write loop
for(int i = 0; i < connections.length; i++){
  if (readyToWrite[i])
  // do write action
}
as a clarification: each of those loops I mentioned above lives in it's own thread...
Avatar of oleber

ASKER

aaronblum, this is a killing machine process. I will be using unnecessary CPU.
true - it does use CPU continuously, the only fix to that would be to add a delay to the multiplexor and have it signal when action needs to be taken.  Use signal listerners in each of the children's threads so that they may sleep when nothing needs doing.  But point well taken, the solution I offered uses up CPU needlessly.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you looked into the Grizzly server?

https://grizzly.dev.java.net/
Grizzly 2.0 is out now, and some examples are coming out:

http://blogs.sun.com/oleksiys/entry/grizzly_2_0_streamreader_streamwriter