SocketChannel的超时不起作用

编程入门 行业动态 更新时间:2024-10-27 04:36:02
本文介绍了SocketChannel的超时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用 SocketChannel 并使其读/写方法超时。我试图为拥有我的 SocketChannel 的Socket设置超时,如下所示:

I want to use a SocketChannel and to have a timeout for its read/write methods. I've tried to set a timeout for the Socket that owns my SocketChannel like this:

channel.socket().setSoTimeout(TIMEOUT);

但这不起作用。还有其他解决方案吗?

but that doesn't work. Is there any other solution?

推荐答案

根据这个文章,SocketChannel不会因读取操作而超时,但是你可以从读取操作中获得此效果另一种方式。

According to this article, SocketChannel will not timeout for its read operation but you can get this effect from reading from the channel in another way.

SocketChannel socketChannel; socketChannel.socket().setSocketTimeout(500); InputStream inStream = socketChannel.socket().getInputStream(); ReadableByteChannel wrappedChannel = Channels.newChannel(inStream);

从wrappedChannel读取将根据您设置的socketTimeOut超时。

reading from the wrappedChannel will timeout according to the socketTimeOut you have set.

更多推荐

SocketChannel的超时不起作用

本文发布于:2023-05-31 12:01:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/391375.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   SocketChannel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!