prathmesh.kallurkar
prathmesh.kallurkar

Reputation: 5686

Java poll on network connections

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 ??

Upvotes: 4

Views: 2310

Answers (2)

Alexey Kutuzov
Alexey Kutuzov

Reputation: 689

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.

Upvotes: 2

ldam
ldam

Reputation: 4585

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

Upvotes: 1

Related Questions