Java写的TCP/IP通讯的问题:

yadang 2002-01-27 10:46:24
用Java的Socket类写TCP/IP通讯,用read()函数时要阻塞。有没有什么方法,让read()函数在一定的时间内没有接受到数据就返回?
...全文
126 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xmvigour 2002-01-28
  • 打赏
  • 举报
回复
阅~
yadang 2002-01-28
  • 打赏
  • 举报
回复
jdk 1.4 的new io是个什么东东??怎么没见过???
xiaozhubenben 2002-01-28
  • 打赏
  • 举报
回复
插一句,用 jdk 1.4 的new io吧,如果你需要支持大规模的并发连接(500-1000)的话。
skyyoung 2002-01-28
  • 打赏
  • 举报
回复
setSoTimeout
public void setSoTimeout(int timeout)
throws SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Parameters:
timeout - the specified timeout, in milliseconds.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK 1.1
See Also:
getSoTimeout()
oldcat0076 2002-01-28
  • 打赏
  • 举报
回复
同意楼上
苏格拉顶777 2002-01-27
  • 打赏
  • 举报
回复
用Socket.setSoTimeout来设置超时。

23,409

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧