看板java
标 题Re: [问题] ServerSocket.accept() 要怎麽强制结束?
发信站白色情迷 (Sat Jun 3 00:11:27 2006)
转信站ptt!ctu-reader!ctu-peer!news.nctu!netnews.csie.nctu!news.ee.ttu!news.n
※ 引述《[email protected]》之铭言:
> 问题是 ServerListener 这条 thread 里的 ServerSocket.accept()
> 会一直 hang 在那里, 直到有 client 连进来才回传,
> 导致无法马上执行到之後的 if (stopped) 那行程式码,
> 请问我要怎麽写, 才能让 main 去结束 ServerListener? (除了用 System.exit(0))
> 谢谢!!
试一下 ServerSocket 的 setSoTimeout():
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 call to accept() for this
ServerSocket will block for only this amount of time. If the timeout expires,
a java.net. SocketTimeoutException is raised, though the ServerSocket 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:
JDK1.1
See Also:
getSoTimeout()
--
╭╢师大资讯。白色情迷╟┬─ ⊙ Origin : snow.ice.ntnu.edu.tw
├╨→ 140.122.77.49←╜╰─ ☆ From : alg.csie.ntnu.edu.tw