Re: [問題] ServerSocket.accept() 要怎麼強制結束?
※ 引述《eliang.bbs@ptt.cc》之銘言:
> 問題是 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
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章