In Need of Help for Server/Client Non-Blocking I/O in Java or C++!!!

Asparagus

Senior member
Aug 16, 2001
284
1
81
Hello all,

I need to write a program in Java (or C++) that allows a server and client to talk with one another asynchronously. I need to be able to let the client send multiple messages in a row to the server. Then, I need the server to write back to the client as many times as it wants.

I have looked at a ton of webpages that talk about "Non-blocking I/O" for Java - which have given me some help. I've downloaded some example programs, and I've tried modifying them to do what I want, but nothing has worked so far. I've waded through all of the Java.nio man pages...

I've gotten the programs to write back and forth - taking turns. But this isn't what I want! I need the client to write a message to the server. Then I need to be able to send ANOTHER message to the server BEFORE getting a response back from the server. Something like this:

Client: Red
Client: Blue
Client: Green
Client: How are you?
Server: Fine.
Server: How are you?
Client: Great!

Does anyone have any idea how to make this happen? The code I've found online seems to be more complex than I want... Please help me!!!

 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Do you have a required mode of communication? Like sockets or something?

By non-blocking, do you mean that one side must be able to check if there is input from the other side before trying to read? If so, BufferedReader in java (which you would presumeable wrap around a socket or stdin) supports this. Well, I don't know how easy it would be with stdin, threads might be necessary.
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Threading + the traditional Input/Output streams make what you want to do fairly trivial. The network related code for both client and server can be done in under 100 lines.

The new way to do it would be NIO, but it sounds much more complex than what is needed.

A Basic outline:

Main program - creates CommunicationClass (which implements runnable)
Thread thread = new Thread(communicationInstance);
thread.start();

Now your CommunicationClass run method should
1) Open socket connection
2) get OutputSTream (probably wrapped in a BufferedOutputStream)
3) get InputStream (probably wrapped in a BufferedInputStream)
4) Go into read loop

CommunicationClass read loop looks like:
while(connected) {
String line = bufferedInputStream.readLine();
//do stuff with what the server sent you
}

Your CommunicationClass should have a write method which your Main program can call at any time which does something like:
bufferedOutputStream.write("Stuff");
bufferedOutputStream.flush(); //make sure "Stuff" gets sent right now

And tada, you have a dedicated thread for reading that will read whenever something happens to arrive, and you are piggy-backing on the main thread for writing. If you want you can also create a dedicated thread for writing, but it isn't really necessary unless you want to buffer your outputs
 

Asparagus

Senior member
Aug 16, 2001
284
1
81
Yes, I'm working with sockets...

Also, yes, blocking is just what you said it is...

The problem is that when I write from the client to the server, it "blocks" the writer. Until the server has read the message and sent a response back to the client, the client can't write again. This is obviously NOT what I want. I need the client to write to the server, and then while it's waiting for a response, keep writing more to the server...

I've been avoiding having to use threads - Java has a "Non-Blocking I/O Library" called java.nio. I've tried using it - using channels, selectors, yada, but it's getting me confused.

I might be stuck having to use threads. If I was to go this route, how should I go about it? On the client side, maybe start a new thread (with it's own BufferedReader/PrintWriter) for each message I want to send to the server? When I start each thread, create a timeout for it...so if I don't get a response back from the server within a certain time, I know to just close that particular thread?

I'd have to make the server able to handle multiple incoming threads... Hmm...it's a thought. Any ideas?
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: Asparagus
Yes, I'm working with sockets...

Also, yes, blocking is just what you said it is...

The problem is that when I write from the client to the server, it "blocks" the writer. Until the server has read the message and sent a response back to the client, the client can't write again. This is obviously NOT what I want. I need the client to write to the server, and then while it's waiting for a response, keep writing more to the server...

I've been avoiding having to use threads - Java has a "Non-Blocking I/O Library" called java.nio. I've tried using it - using channels, selectors, yada, but it's getting me confused.

I might be stuck having to use threads. If I was to go this route, how should I go about it? On the client side, maybe start a new thread (with it's own BufferedReader/PrintWriter) for each message I want to send to the server? When I start each thread, create a timeout for it...so if I don't get a response back from the server within a certain time, I know to just close that particular thread?

I'd have to make the server able to handle multiple incoming threads... Hmm...it's a thought. Any ideas?


Read my edit above for a quick overview of what the code looks like.

Traditional stream servers utilize 1 thread per client, the server loops are even simpler looking something like:
while (true) {
Socket incoming = s.accept();
new ClientHandler(incoming).start();
}

In this case ClientHandler extends Thread. I'm attaching the basics of the ClientHandler class so you get an idea.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Asparagus
The problem is that when I write from the client to the server, it "blocks" the writer. Until the server has read the message and sent a response back to the client, the client can't write again. This is obviously NOT what I want. I need the client to write to the server, and then while it's waiting for a response, keep writing more to the server...

Something's wrong there. Writing to a socket should not block. Are you sure you aren't trying to read after doing a write and blocking there? Do you have a debugger available?