开发者

Android: OutputStreamWriter didn't send the data after flush (socket)

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 开发者_StackOverflowis no data will be sent to server.

OutputStreamWriter out = new OutputStreamWriter(sock.getOutputStream());

out.write(data);
out.close();

Is fine. But why?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜