Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JAVA SOCKETS (TCP Server/Client Application) error (random)

843810May 15 2006 — edited Jun 16 2006
Hi all !

(first of all I apologize for my poor english, I'm a french student)

Here is a sum up :

I write a chat application with server and client. But from time to time (NOT all the time, but often), the data sent in second is not received by the client (or server, whatever). If I use the PrintStream.checkError() function, it says there is an error, but I do not know when and where.
I have (almost) TWO behaviors :
1) everything is perfect, I can send data when I want and it does not fail
2) the SECOND sending is NEVER received (never received or.... never sent ????? Here is the question), whereas the first sending succeeds all the time


Here is a far more detailed explanation :

I think I have kind of a big problem with sockets (in fact, "flush" does not work all the time... it seems to be very random)
I'll explain the goal of my app, then how it (should) works, and finally where the problem is. I really hope someone will be able to put the light on my mistake... !

THE GOAL :
-----------------
I aim to write a kind of "msn" application in Java (a "great" goal lol but hope is a good thing :)).
So I created 2 apps : a server app, accepting client connections, and a client app, to connect to a server.
I test my apps localy : I run the server, then launch a client and then connect this client to the server, and finally sends text from the client (like you would do in msn, by validating a JTextField)

HOW IT (should) WORKS :
-----------------------------------
On server side, I have a server class that waits for connections, and that has some functions like sayToAll() etc. I also have a client class (thread, implementing Runnable), and in run() I have an infinite loop (based on the boolean "connected") where I read data.
I made some functions "synchronized" but I'm not sure if I did a good thing (for exemple the "read buffer" is synchronized, but the "send data to buffer" is not.... I'm not sure if its good or not)

There is a beginning of protocol : when socket is created (client connected), the client sends a LOGIN (like : "LOGIN myLogin"), then a PWD (like : "PWD myPass") and finally a NICK (like : "NICK myNick") messages to identify correctly to the server.

If everything were well written, the server should send a KILL message if he does not receive the client messages in that order precisely.

Then, the client can type in text in a JTextField, and it is sent to the server via the SAY message. When receiving it, the server sends the data to all the clients (SAY message) and then it is appended to the end of the JTextPane on the client.


THE PROBLEM :
---------------------
It seems to happen randomly, but sometimes... I dunno how to explain... When servers listens to the client, the normally blocking function to read the "in" buffer does not block and the connection seems to be lost with the client because the server debug info says "sending blablabla to the client", but the client does not receive anything. And the most weird : the server AND the client do not seem to detect a deconnection.
If I use flush() (or checkError()), checkError() indicates me that an error occured, but I have absolutely no idea WHERE and WHEN.
In fact, sometimes I can send properly the first message (server's reading function blocks, waiting for data to read), but when I try to send the second message, it fails, and then the server loops on trying to read the "in" buffer (it is no longer blocking).
When I do not use flush() or checkError(), I have the same problem from time to time.

The weird thing is : sometimes it works PERFECTLY (I cand send as much text as I want to the server, and it echoes it), and sometimes it fails at the second message sending.


I tried to look at many examples on the web, and I tried everything but its always the same thing ! Even for the "in" and "out" classes : BufferedReader, DataInputStream, DataOutputStream, PrintStream and PrintWriter. I tried with classes extending Thread, and now with classes implementing Runnable, but... Nothing works ALL THE TIME !!!

Please, if you have any Idea, I would REALLY appreciate any help, because I don't know why it is a random error :'(

(I can post my code if you need it to solve the problem. Juste ask :))
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 14 2006
Added on May 15 2006
2 comments
587 views