开发者

Write to XMLSocket without adding \r\n

I'm trying to communicate with an XMLSocket server: i used to do that with a flash application, but now I'm trying to do it with java. Right now the code I'm using is

xmlsock = new Socket("127.0.0.1", 1012);
out = new PrintWriter(xmlsock.getOutputStream(), true);
out.println(this.jTextField1.getText()+'\u0000');

The problem is that, obviously, using println to write in the socket adds at the end of each line \r\n bytes, that make the server read only the first line i send to it. Using print instead of println doesn't seem to send anything at all ( no string received by the server). So, is there a way to send strings to the server, without adding the \r\n at the end of 开发者_开发知识库the string?


Have you tried calling out.print() instead of out.println()? (edit oh durr, I see you mention that. Well read the next paragraph ...)

You can use out.flush() to force buffers to be dumped without affecting the actual written stream of characters.

out.print(this.jTextField1.getText()+'\u0000');
out.flush();


You are using auto flush on new line in PrintStream. This is why you don't need to flush() the data. However if you want to avoid sending newlines you need to do the flush() yourself.


[PrintWriter(OutputStream out, boolean autoFlush)](http://download.oracle.com/javase/1.4.2/docs/api/java/io/PrintWriter.html#PrintWriter(java.io.OutputStream, boolean))

autoFlush - A boolean; if true, the println() methods will flush the output buffer

So you have to use print() and flush() together:

out.print(this.jTextField1.getText()+'\u0000');
out.flush();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜