New line and StringBuilder
I've made a client-server chat program - many clients to one server.
To end it off, I made a JTextArea to store information about all clients currently connected to the server.
My method is not the best, but in theory should work.
I iterate through an ArrayList, dumping all objects (which are Strings) into the StringBuilder and then broadcast the StringBuilder object.toString() to all clients.
By itself, like that, it does work fine. I get a single line of all the clients.
My problem is that I want a new line after each user name, but where ever I try adding either "\n" or System.getProperty("line.separator") it will only ever broadcast the first entry in the ArrayList each time.
I got this issue each time I tried other approaches such as broadcasting straight out of the array, or basic concatenation.
My question is: Do PrintWriter's read new lines as end or whatever?
Heres a brief example of when my program goes wrong:
Code :
while (arrayIterator.hasNext()) {
buffer.append(arrayIterator.next().toString());
//buffer.append(NEW_LINE);
when I uncomment NEW_LINE and add it into the buffer, it will only print the first arraylist element when using the printwriter to send.(new line = System.getProperty("line.separator"))
Broadcast with:
Code :
for (PrintWriter writer : writers) {
writer.println("NAMES" + buffer.toString());
}
(I tried sending over the ArrayList from server to clients with a static getter method so i could dump straight from the client side into the JTextArea but always kept returning empty)
-Sorry for the wall of text.
Re: New line and StringBuilder
This is probably a long shot, but where is the end to your while loop?
Are you adding the NEW_LINE every time?
Re: New line and StringBuilder
Code :
while (arrayIterator.hasNext()) {
buffer.append(arrayIterator.next().toString());
//buffer.append(NEW_LINE);
}
Sorry, missed out the last bracket, it ends there.
When i leave it commented, it will add the all the clients onto my JTextArea on a single line,
If I uncomment it, it prints the first elements and then nothing.
Re: New line and StringBuilder
-Edit, by just printing out the StringBuilder with "\n" added, results do print out on new lines. So its bound to be a PrintWriter issue perhaps?
Re: New line and StringBuilder
How are the clients reading the input? Are they expecting and reading a single line, or multiple lines from the server? The println should not evaluate the new line characters in the string, so I am thinking of the scenario where all the data is being sent, but the client reads the data in a manner which doesn't capture anything after the first new line.
Re: New line and StringBuilder
A thread(with a runnable target) is constantly running checking for server responses:
where line = in.readLine();
Code :
else if (line.startsWith("NAMES")) {
clientLister.setText(line.substring(5));
}
Server sends message starting with NAMES, so it receives the input - "NAMES".
As I'm sending the whole buffer in one, I only need to add this to the JTextArea once.
But If I try and place \n or line.separator into the buffer it stops printing everything that should come after the first "\n".
Re: New line and StringBuilder
It seems your problem is then client side. The server will send content like this
Code :
NAMES name1
name2
name3
name4
The client then reads these line by line, the first line the only one containing the "NAMES" tag, and thus the only one parsed and interpreted correctly.
Re: New line and StringBuilder
So do StringBuilders send it segments?
I took on what you said and tried all the following:
A)added "NAMES" onto each arraylist element
B)add Names before each String in the StringBuffer
and i either get the same result as I did to start with
or i got name1NAMES name2
Re: New line and StringBuilder
Quote:
Originally Posted by
newbie
So do StringBuilders send it segments?
No. You are calling read line, which reads the stream until the next line delim is found. From what I understand based upon the code and explanations posted, you are reading in segments - each segment being a line. If you print a bunch of lines server side, the client must read a bunch of lines to get all the information. Rather than fuss with all the new lines, try the following experiment: instead of appending a new line after each element, append some other delimiter (say for example a semi-colon or tab space). Then when the client reads the line that begins with NAMES, split the line based upon this delimiter. A bit crude of a hack, but could be a short term solution (or at least something to pick apart the code)
Re: New line and StringBuilder
Your advice worked great!
I really do appreciate the time and help you gave me, this had been bothering me for ages.
Thanks a million!