简体   繁体   English

java套接字/输出流写入:它们阻塞吗?

[英]java socket / output stream writes : do they block?

If I am only WRITING to a socket on an output stream, will it ever block?如果我只在输出流上写入套接字,它会阻塞吗? Only reads can block, right?只有读取可以阻塞,对吗? Someone told me writes can block but I only see a timeout feature for the read method of a socket - Socket.setSoTimeout() .有人告诉我写可以阻塞,但我只看到套接字读取方法的超时功能 - Socket.setSoTimeout()

It doesn't make sense to me that a write could block.对我来说,写可能会阻塞是没有意义的。

A write on a Socket can block too, especially if it is a TCP Socket.对 Socket 的写入也可能会阻塞,尤其是在它是 TCP Socket 的情况下。 The OS will only buffer a certain amount of untransmitted (or transmitted but unacknowledged) data.操作系统只会缓冲一定数量的未传输(或传输但未确认)的数据。 If you write stuff faster than the remote app is able to read it, the socket will eventually back up and your write calls will block.如果你写东西的速度比远程应用程序读取的速度快,套接字最终会备份,你的write调用将阻塞。

Responding to these followup questions:回答这些后续问题:

So is there a mechanism to set a timeout for this?那么有没有一种机制可以为此设置超时? I'm not sure what behavior it'd have...maybe throw away data if buffers are full?我不确定它会有什么行为......如果缓冲区已满,可能会丢弃数据? Or possibly delete older data in the buffer?或者可能删除缓冲区中的旧数据?

There is no mechanism to set a write timeout on a java.net.Socket.没有在 java.net.Socket 上设置写入超时的机制。 There is a Socket.setSoTimeout() method, but it affects accept() and read() calls ... and not write() calls.有一个Socket.setSoTimeout()方法,但它影响accept()read()调用......而不是write()调用。 Apparently, you can get write timeouts if you use NIO, non-blocking mode, and a Selector, but this is not as useful as you might imagine.显然,如果您使用 NIO、非阻塞模式和 Selector,您可以获得写入超时,但这并不像您想象的那么有用。

A properly implemented TCP stack does not discard buffered data unless the connection is closed.除非连接关闭,否则正确实现的 TCP 堆栈不会丢弃缓冲的数据。 However, when you get a write timeout, it is uncertain whether the data that is currently in the OS-level buffers has been received by the other end ... or not.但是,当您收到写入超时时,不确定当前位于操作系统级缓冲区中的数据是否已被另一端接收...... The other problem is that you don't know how much of the data from your last write was actually transferred to OS-level TCP stack buffers.另一个问题是您不知道上次write的数据中有多少实际传输到了操作系统级别的 TCP 堆栈缓冲区。 Absent some application level protocol for resyncing the stream * , the only safe thing to do after a timeout on write is to shut down the connection.缺少一些用于重新同步流*的应用程序级协议,在write超时后唯一安全的做法是关闭连接。

By contrast, if you use a UDP socket, write() calls won't block for any significant length of time.相比之下,如果您使用 UDP 套接字,则write()调用不会阻塞很长时间。 But the downside is that if there are network problems or the remote application is not keeping up, messages will be dropped on the floor with no notification to either end.但不利的一面是,如果出现网络问题或远程应用程序跟不上,消息将被丢弃在地板上,而不会通知任何一方。 In addition, you may find that messages are sometimes delivered to the remote application out of order.此外,您可能会发现消息有时会乱序传递到远程应用程序。 It will be up to you (the developer) to deal with these issues.由您(开发人员)来处理这些问题。

* It is theoretically possible to do this, but for most applications it makes no sense to implement an additional resyncing mechanism on top of an already reliable (to a point) TCP/IP stream. * 理论上可以做到这一点,但对于大多数应用程序来说,在已经可靠(到一定程度)的 TCP/IP 流之上实现额外的重新同步机制是没有意义的。 And if it did make sense, you would also need to deal with the possibility that the connection closed ... so it would be simpler to assume it closed.如果它确实有意义,您还需要处理连接关闭的可能性......所以假设它关闭会更简单。

The only way to do this is to use NIO and selectors.这样做的唯一方法是使用 NIO 和选择器。

See the writeup from the Sun/Oracle engineer in this bug report: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4031100请参阅此错误报告中来自 Sun/Oracle 工程师的文章: https ://bugs.java.com/bugdatabase/view_bug.do?bug_id=4031100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM