Search code examples
javasocketsmethodsjtextfield

Java: How to figure out whether .getInputStream() receives text or not?


I'm programming chat tool. A JTextField reads the text, which I get by .getInputStream() from the client socket. But now I have to decide whether a new character is part of a message (string which sent to the socket) or the first character of a new message.

My recent idea is to figure out whether my .getInputStream()-method at the client socket is receiving text in one moment or not. Is there a method to find this out?


Solution

  • Instead of sending pure text, you could send a packet of data... send the size of the message string (as an encoded integer), followed by the message as text. Or, as subham says, just use a string encoding function to send whole strings at a time. That will do the buffering for you. You should use built-in functions to determine those boundaries rather than doing so yourself.