是否可以将换行符写入套接字的输出流?
我有一个运行良好的 Socket,但是是否可以向 Socket OutputStream 写入换行符?
这是我尝试过的:
InputStream input = clientSocket.getInputStream();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.write("Hello" + "\r\n");
out.write("People");
out.flush();
I have a Socket
that works well, but is it possible to write a newline
to the Socket
OutputStream?
This is what I've tried:
InputStream input = clientSocket.getInputStream();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.write("Hello" + "\r\n");
out.write("People");
out.flush();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“换行符”是一个基于文本的概念。
OutputStream
是一个基于二进制的概念。如果您要将文本写入套接字,则应该使用某种描述的
Writer
,例如OutputStreamWriter
。然后,您可以将其包装在具有适当的newLine()
方法的BufferedWriter
中。如果您不向套接字写入文本,那么“换行”并没有多大意义。
"newline" is a text-based concept. An
OutputStream
is a binary-based concept.If you're writing text to the socket, you should use a
Writer
of some description, e.g. anOutputStreamWriter
. You can then wrap that in aBufferedWriter
which has an appropriatenewLine()
method.If you're not writing text to the socket, then "new line" doesn't really make much sense.