Java:忽略输入流 - 缓冲区会溢出并发生不好的事情吗?

发布于 2024-08-02 12:47:07 字数 253 浏览 2 评论 0原文

我有一个客户端连接到我的服务器。客户端向服务器发送一些我不关心的消息,如果我不打算使用它们,也不想浪费时间解析其消息。我使用的所有 i/o 都是简单的 java i/o,而不是 nio。

如果我创建输入流但从未从中读取数据,该缓冲区是否会填满并导致问题?如果是这样,我可以做些什么或者可以设置一个属性来让它丢弃它看到的数据吗?

现在如果服务器根本不创建输入流怎么办?这会在客户端/发送端造成任何问题吗?

请告诉我。

谢谢, 吉布

I have a client connecting to my server. The client sends some messages to the server which I do not care about and do not want to waste time parsing its messages if I'm not going to be using them. All the i/o I'm using is simple java i/o, not nio.

If I create the input stream and just never read from it, can that buffer fill up and cause problems? If so, is there something I can do or a property I can set to have it just throw away data that it sees?

Now what if the server doesn't create the input stream at all? Will that cause any problems on the client/sending side?

Please let me know.

Thanks,
jbu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南笙 2024-08-09 12:47:07

当您接受来自客户端的连接时,您将获得一个InputStream。如果您不从该流中读取数据,客户端的数据将被缓冲。最终,缓冲区将被填满,并且客户端在尝试写入更多数据时将被阻塞。如果客户端在读取服务器响应之前写入所有数据,那么最终会出现非常典型的死锁情况。如果您确实不关心来自客户端的数据,只需读取(或调用skip)直到 EOF 并删除数据。或者,如果它不是标准请求/响应(如 HTTP)协议,则启动一个新线程来不断读取流以防止其备份。

When you accept a connection from a client, you get an InputStream. If you don't read from that stream, the client's data will buffer up. Eventually, the buffer will fill up and the client will block when it tries to write more data. If the client writes all of its data before reading a response from the server, you will end up with a pretty classic deadlock situation. If you really don't care about the data from the client, just read (or call skip) until EOF and drop the data. Alternatively, if it's not a standard request/response (like HTTP) protocol, fire up a new thread that continually reads the stream to keep it from getting backed up.

离笑几人歌 2024-08-09 12:47:07

如果您没有从客户端获得有用的数据,那么允许它连接有什么意义呢?

If you get no useful data from the client, what's the point of allowing it to connect?

挽你眉间 2024-08-09 12:47:07

我不确定在 Java 中从不读取缓冲区的含义 - 我猜操作系统最终会停止接受该套接字上的数据,但我不确定。

为什么不直接调用 你的InputStream的skip方法偶尔会使用大量数据,以确保你丢弃数据?

I'm not sure of the implications of never reading from a buffer in Java -- I'd guess that eventually the OS would stop accepting data on that socket, but I'm not sure there.

Why don't you just call the skip method of your InputStream occasionally with a large number, to ensure that you discard the data?

猫腻 2024-08-09 12:47:07
InputStream in = ....
byte[] buffer = new byte[4096] // or whatever
while(true)
  in.read(buffer);

如果您接受连接,则应该读取数据。告诉你真相,我从未见过(或可以预见)这种情况(忽略所有数据的服务器)可能有用。

InputStream in = ....
byte[] buffer = new byte[4096] // or whatever
while(true)
  in.read(buffer);

if you accept the connection, you should read the data. to tell you the truth i have never seen (or could forsee) a situation where this (a server that ignores all data) could be useful.

疯了 2024-08-09 12:47:07

我认为一旦您接受请求,您就会获得InputStream,因此,如果您不确认该请求,底层框架(即tomcat)将丢弃该请求(经过一段时间后)时间)。

问候。

I think you get the InputStream once you accept the request, so if you don't acknowledge that request the underlying framework (i.e. tomcat) will drop that request (after some lapsed time).

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文