Android:OutputStreamWriter在刷新(套接字)后没有发送数据
OutputStreamWriter out = new OutputStreamWriter(sock.getOutputStream());
out.write(data);
out.flush();
袜子——插座; data - 字符缓冲区;
因此,如果我不关闭“out”或“sock”,则不会有数据发送到服务器。
OutputStreamWriter out = new OutputStreamWriter(sock.getOutputStream());
out.write(data);
out.close();
很好。但为什么?
OutputStreamWriter out = new OutputStreamWriter(sock.getOutputStream());
out.write(data);
out.flush();
sock - Socket; data - buffer of chars;
So, if I wouldn't close the "out", or "sock" then there is no data will be sent to server.
OutputStreamWriter out = new OutputStreamWriter(sock.getOutputStream());
out.write(data);
out.close();
Is fine. But why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
flush()
仅将 Java/Android 的应用程序级缓冲区刷新到 TCP 堆栈。一旦到达那里,它可能会被操作系统的 TCP 实现进一步延迟,操作系统试图有效地利用传输的数据包并等待更多数据。 (如果需要更多详细信息,请搜索“Nagle 算法”。)无法在每次写入套接字时刷新 TCP 缓冲区,但可以通过设置
TCP_NODELAY
请参阅Socket 上的 Java ObjectOutputStream 未刷新() 有关该主题的更多信息。
flush()
only flushes Java's/Android's application level buffer to the TCP stack. Once there, it can be further delayed by the TCP implementation of the OS that tries to make efficient use of transmitted packets and waits for more data. (Search for "Nagle's algorithm" if you need more detail.)There is no way to flush a TCP buffer per write to a socket, but the socket can be configured to immediately send all data in the buffer when possible, by setting
TCP_NODELAY
See Java ObjectOutputStream on Socket not flush()ing for more information on the topic.