Hi Lucas,
Yes, it is an interesting issue. I'm not much into sockets and all, but out of curiosity, I tried to run the code and faced the same issue.
And I would really like to know why this happens
Meanwhile, I tried to put a counter as part of message (and the counter is increased during each iteration), and below is what I got:
1) When I run the code on a new port (e.g. 5555), then for first time, I get this message 3 times. The last counter on console is 3, however, the counter on browser (which we write via PrintWriter) is 1.
2) If I refresh browser page (i.e. send request again), then counter is 4 on both - console and browser. After this, it remains in sync (on console and browser).
3) I checked after inserting print statements and found that the loop is actually executing multiple times. The only reason I can think of is due to the way browser sends the request, accept method is being invoked more than once (but then, I'm still not sure why we do not get last counter's value '3' on browser).
4) Once those counter values are in sync (i.e. after first refresh), this problem never occurs with that particular port (I also tried restarting the program, in which case, the values were in sync right from counter = 1). To reproduce the problem on the same port, I had to restart the machine.
I hope someone knowledgeable in this might find above analysis helpful.