4

I am writing a program in Java where I have opened 256 network connections on one thread. Whenever there is any data on a socket, I should read it and process the same. Currently, I am using the following approach :

while true
do
   iterate over all network connections
   do
         if non-blocking read on socket says there is data
         then
               read data
               process it
         endif
   done
   sleep for 10 milli-seconds
done

Is there a better way to do the same on Java ?? I know there is a poll method in C/C++. But after googling for it, I did not get concrete idea about Java's polling. Can somebody explain this ??

prathmesh.kallurkar
  • 5,468
  • 8
  • 39
  • 50

2 Answers2

1

The java.nio package sounds right for what you want to do. It provides ways to perform asynchronous IO.

ldam
  • 4,412
  • 6
  • 45
  • 76
  • 1
    It also provides ways to perform *non-blocking* and *multiplexed* I/O, which sound more like what he wants to do. – user207421 Mar 19 '13 at 07:26
1

Take a look to http://netty.io/ (this is a non-blocking framework to build network application on java). https://community.jboss.org/wiki/NettyExampleOfPingPongUsingObject - 'hello world' on netty.

Alexey Kutuzov
  • 679
  • 8
  • 22