Java poll on network connections

倾然丶 夕夏残阳落幕 提交于 2019-12-23 19:34:10

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/15493272/java-poll-on-network-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!