开发者

Java Sending data through a socket - why do I need to put a newline at the end of a String?

So, while I now get why the Outputstream connected to a socket needs to be flushed when data should be sent immediately, I still don't get why I need to put a newline at the end of the String.

This works:

// writ开发者_C百科er is a PrintWriter
writer.println("Hello World!");

This doesn't:

Writer.print("Hello World!");
writer.flush;


You should define work and doesn't work more precisely. What's the protocol between the client and the server? What do you expect to happen and what do you observe?

I suspect the server is simply waiting for a newline character to appear in its input before doing anything. If that's the case, then of course flushing the writer at client side before sending the newline won't help.


If you are reading with a BufferedReaders readLine method as per your previous question, readLine won't complete unless it finds the newline character(s).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜