Re: [問題] ServerSocket.accept() 要怎麼強制結束?

看板java作者時間19年前 (2006/06/03 02:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《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
文章代碼(AID): #14W7pn00 (java)
文章代碼(AID): #14W7pn00 (java)