network tcp client side connection
i'm working on a client/server application, and the client must keep listening from data received by the server, even if the client hasn't send any requests. I would like to have some information on how to persist the client socket to keep listening to the server incoming information. Usually on the server side there is a infinite while loop. Do i have to do the same thing on the client side ? Thanks for help, or if there is any开发者_StackOverflow社区 good tutorial that i can follow on client/server.
Well, that's simple -- instantiate a java.net.Socket, and use it.
http://download.oracle.com/javase/6/docs/api/java/net/Socket.html
For example get the associated stream via getInputStream(), and then read() bytes from the InputStream.
- javadoc says "If the channel is in non-blocking mode then the input stream's read operations will throw an IllegalBlockingModeException."
- so I guess that, if the channel is in blocking mode, then the input stream's read method will block your thread (thus you won't need any infinite loop in your code)
精彩评论