Java的Socket的问题:

yadang 2002-01-27 11:11:06
用Java的Socket类写TCP/IP通讯,用read()函数时要阻塞。有没有什么方法,让read()函数在一定的时间内没有接受到数据就返回?
...全文
105 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
weilinwu 2002-01-28
  • 打赏
  • 举报
回复
<CORE JAVA第二册(高级)>哪里有下载??

买一本吧,yadang(亚当条)大哥,也就70、80元,纸版的书有时候比电子版的书好用多了!
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()
meixiaolong 2002-01-28
  • 打赏
  • 举报
回复
java.net.Socket类有setTimeOut(int time)方法设置多少时间之内如没有读到消息会抛出违例
crazybaw 2002-01-28
  • 打赏
  • 举报
回复
你到这里去下载吧
http://202.192.154.5/ebook/moreupdate.asp?offset=40

这个页面只是其中的一页,你好好找找就能找到那本书了

全名:〈Java 2 核心技术卷2:高级特性〉
Norwaywoods 2002-01-27
  • 打赏
  • 举报
回复
详见:《CORE JAVA第二册(高级)〉的关于线程的章节。
yadang 2002-01-27
  • 打赏
  • 举报
回复
楼上的兄弟:能不能给个详细的资料?
Norwaywoods 2002-01-27
  • 打赏
  • 举报
回复
用线程,并且设置Socket的超时(TimeOut)。如下例:
ConnServer con = new ConnServer(ip , port , frame);//这是一个连接服务器的操作,采用了线程和超时设置,可以做到连接时不会阻塞,并且在一段时间连接不上自动返回。
Thread t = new Thread(con);
t.start();
try
{
t.join(timeout);
}

catch(InterruptedException e)
{
System.out.println("Error: " + e);
}
yadang 2002-01-27
  • 打赏
  • 举报
回复
<CORE JAVA第二册(高级)>哪里有下载??

23,407

社区成员

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

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