简体   繁体   English

从套接字读取n秒

[英]Read from a socket for n seconds

I'd like to put a timeout reading a socket, i found this in C++ and i'd like to convert it on Java. 我想把读取套接字的超时,我在C ++中找到它,我想在Java上转换它。

int readable_timeo(SOCKET fd, int sec){
fd_set rset;
struct timeval tv;


FD_ZERO(&rset);
FD_SET(fd, &rset);

tv.tv_sec = sec;
tv.tv_usec = 500000;

return (select(0, &rset, NULL, NULL, &tv));}

Any suggestion? 有什么建议吗?

Thanks for the help! 谢谢您的帮助!

The simple equivalent in Java is Socket.setSoTimeout(). Java中的简单等价物是Socket.setSoTimeout()。 The equivalent of the code you posted, which isn't the simplest, is Selector.select(). 您发布的代码(也不是最简单的代码)相当于Selector.select()。

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

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