java socket / output stream写道:它们阻塞了吗? [英] java socket / output stream writes : do they block?

查看:284
本文介绍了java socket / output stream写道:它们阻塞了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我只是在输出流上写入套接字,它会阻塞吗?只有读取可以阻止,对吗?有人告诉我写入可以阻止,但我只看到套接字读取方法的超时功能 - Socket.setSoTimeout()



对我来说写一个可以阻塞是没有意义的。

解决方案

在Socket上写一个可以阻止,特别是如果它是TCP套接字。操作系统仅缓冲一定量的未传输(或传输但未确认)数据。如果您编写的东西比远程应用程序能够读取的速度快,则套接字最终会备份,并且您的写入调用将阻止。



回应这些后续问题:


那么是否有机制为此设置
超时?我不确定它有什么
行为...如果缓冲区已满,可能会抛弃
数据?或者可能
删除缓冲区中的旧数据?


没有机制在java.net上设置写入超时。插座。有一个 Socket.setSoTimeout()方法,但它会影响 accept()读取()调用...而不是 write()调用。显然,如果您使用NIO,非阻塞模式和选择器,您可以获得写入超时,但这并不像您想象的那样有用。



正确实施除非关闭连接,否则TCP堆栈不会丢弃缓冲数据。但是,当您获得写入超时时,不确定当前在OS级别缓冲区中的数据是否已被另一端接收...另一个问题是您不知道上一次 write 中有多少数据实际上已转移到OS级别的TCP堆栈缓冲区。没有用于重新同步流 * 的某些应用程序级协议,在 write 超时后唯一安全的做法是关闭连接。 / p>

相比之下,如果您使用UDP套接字, write()调用将不会阻塞任何显着长度的时间。但缺点是,如果存在网络问题或远程应用程序没有跟上,消息将被丢弃在地板上,而不会通知任何一端。此外,您可能会发现消息有时会无序传递到远程应用程序。由您(开发人员)来处理这些问题。



*理论上可以这样做,但对于大多数应用程序来说,它不会意味着在已经可靠(到某一点)的TCP / IP流之上实现额外的重新同步机制。如果它确实有意义,你还需要处理连接关闭的可能性......所以假设关闭它会更简单。


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().

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. 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.

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. There is a Socket.setSoTimeout() method, but it affects accept() and read() calls ... and not write() calls. 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.

A properly implemented TCP stack does not discard buffered data unless the connection is closed. 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. 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.

By contrast, if you use a UDP socket, write() calls won't block for any significant length of time. 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. 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.

这篇关于java socket / output stream写道:它们阻塞了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆