How to communicate asynchronously with client/server?

Hi, I've been working on a client/server project based off of the KnockKnock Server/Client example (multiclient one) and I'm wondering how I can make it so that both sides don't wait around for input before doing anything else. Currently, since I've followed the example and done something likewhile((inputLine=in.readLine())!=null){

/*do other stuff*/

}

this causes both sides to wait until receiving something before sending something. This is not a good way for my program to run. How can I change it so that it doesn't wait on the input before continuing in the loop? I tried doing something like

while(running){

inputLine=in.readline();

if(inputLine!=null)//process input

/*do other stuff*/

}

but this still waits on the input part. Any ideas? Not sure if asynchronously is the right word, but I think it covers the right idea.

Oh, I saw something about using two sockets while searching some more on google. How effective would that be?

Message was edited by:

iofthestorm

[1428 byte] By [iofthestorma] at [2007-11-27 6:39:43]
# 1

There are several choices:

(a) use a dedicated reading thread and a dedicated writing thread and feed them via queues, so the rest of your application can get on with its life

(b) read with a short timeout

(c) test InputStream.available() before reading and do whatever else you have to do if it returns zero, or less than the amount of data you need in one go

(d) use non-blocking NIO.

ejpa at 2007-7-12 18:08:54 > top of Java-index,Core,Core APIs...
# 2
What is nonblocking NIO? Is that something like socketchannel? I've been googling some more but I can't find a good example of it.
iofthestorma at 2007-7-12 18:08:54 > top of Java-index,Core,Core APIs...
# 3
It is something exactly like SocketChannel. See the NIO Guide to Features and the Tutorial in the JDK documentation.
ejpa at 2007-7-12 18:08:54 > top of Java-index,Core,Core APIs...
# 4
Oh thanks, I decided to use the InputStream.available() and now my program works great. I'll check out SocketChannel maybe later if I end up needing it, since it seems like that is a faster method, but for now this works.
iofthestorma at 2007-7-12 18:08:54 > top of Java-index,Core,Core APIs...