Multithreading with java.io

Greetings to all,

I am using a thread to read messages over a socket which allows a person to type while the thread reads the messages from the other client. the problem is that the thread misses out on every next message, for example if I typed “Hello” the first time and then type “how are you” ? then afterward typed “where are you?”. The Only ones that appears on the other computer are “Hello” and “Where are you?”.

I no idea why it is doing this, do I need to create like a protocol to specify heads and ends of messages for this?

I am using Object[Input/Output]Stream to read and write .

However, I also got to read about java.nio, and think it could be my solution though I need some feedback… because I really want to save sometime

Thanks

You can be reasonable safe to assume there are no bugs in java.io.* and java.net.*, assuming all those websites running on a Java backend actually work.

Don’t even think NIO will solve your problems, because you are having trouble due to your own bugs.

When in trouble: simplify.
Use Data[Input/Output]Stream with readUTF() and writeUTF()+flush(). Print everything to System.out before handling it in your GUI. Once you found that basic network I/O works (and it does!) try to see where your ‘every other’ messages get lost, and fix that bug.

you could try the java tutorials…

http://java.sun.com/docs/books/tutorial/networking/sockets/index.html

this is a great place to start and you,ll have a chat program written in no time.