socket server without *any* polling?
I've started experimenting with creating my own socket servers using Java, and I find the polling-vs.-socket argument ironic because all the Java examples I've seen use polling on the SERVER side. While polling on the client side leads to lower performance (low update frequency), polling on the server can eat up your CPU while effectively doing nothing.
Ok, I'm probably not being clear about this, but most Java examples tend to have a while(true) or equivalent section which just repeatedly checks the state of the socket buffer, waiting for data. This method works, but will eat up 25% of my CPU usage even though my server activity is completely idle. On principle alone, this is unacceptable. I'd like to write (or use) a socket server that is completely event-driven (perhaps this is accomplished with Java exceptions?) and will not burn cycles just waiting for something to happen.
How does ElectroServer (or similar product) deal with this? Does anyone have a Java example that doesn't poll the socket? Help! Thanks!!! :)
Re: socket server without *any* polling?
Quote:
Originally posted by infinite_fish
Ok, I'm probably not being clear about this, but most Java examples tend to have a while(true) or equivalent section which just repeatedly checks the state of the socket buffer, waiting for data. This method works, but will eat up 25% of my CPU usage even though my server activity is completely idle.
Mmm... this sounds a little weird to me, since most of the basic socket servers examples out there are based on the old blocking java.net libraries.
A typical while section could look like this:
PHP Code:
while (!quit)
{
String msg = data_in.readLine();
if (msg == null) quit = true;
if (!msg.trim().equals("EXIT"))
{
data_out.println("Data read: <b>"+msg.trim()+"</b>"+EOF);
data_out.flush();
}
else
{
quit = true;
}
}
Since the readline() method is blocking (stops the execution of the thread until data is received) the application will stay idle until some data is read from the socket, then it will process it and go back to idle.
If the examples you've found are sucking 25% of the cpu when doing nothing then there must be something wrong in that code!
Here's a link to an article I wrote some time ago that might interest you:
http://www.gotoandplay.it/_articles/.../xmlSocket.php
And here's a whole section in my website dedicated to multiplayer games in flash >> http://www.gotoandplay.it/_articles/multiplayerCentral
Also here's a must read for all those interested in developing with the latest java.nio package >> http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf
Hope it helps :)
Lapo