Java socket problem

server how to know the socket is broken?
if the connection is ok, then

1. client force shutdown the program, haven't send any shutdown message
2. network a borken, TCP connection close.

is there any callback function in java, when a socket is close/broken/shutdown to let server do something?

原帖由 raydan 於 2009-3-9 13:25 發表
server how to know the socket is broken?
if the connection is ok, then

1. client force shutdown the program, haven't send any shutdown message
2. network a borken, TCP connection close.

is there any ...


You will get an exception

NetworkExceptions: network cable unplugged, or DNS lookup failed (via the subclass UnknownHostException)
SocketException: peer connection reset, connection refused, timeout (via SocketTimeoutException), or something like that

TOP

exception 要read or write 時先出

我想要的是,當個TCP connection close果時, 個server 會知個connection close
而唔係到須要read or write 先知

TOP

原帖由 raydan 於 2009-3-9 13:54 發表
exception 要read or write 時先出

我想要的是,當個TCP connection close果時, 個server 會知個connection close
而唔係到須要read or write 先知


係一定要read/write 先知,你整個timer定期check 下啦。

TOP

i am using
  1. Socket s = wServer.GetSendSocket();
  2. if(s.isConnected() && !s.isInputShutdown() && !s.isOutputShutdown())
  3. {
  4. // connection valid
  5. }
複製代碼
當個connection disconnect, 都係valid connection, but client side 已經disconnect 左

唔用read/write 有無other way?
如果要用timer, 1 秒要行10次

TOP

提示: 作者被禁止或刪除 內容自動屏蔽

TOP

why not get a exception and do something?
it is the responsibility of client to close the connection when it is exiting.
when the client can not normally exit, exception should be threw since it is abnormal and need some code to handle it.

TOP

我個program 是這樣的

1 個server program, 10 個client

example 1:
當server start 時自動connect clients (example: 8個online), use TCP
server display "8", when 1 client disconnect, client 不會send "shutdown", "exit","bye" 等command, and 個tcp connection 會broken.
now server should display "7" (problem is here now!)
當client disconnect, still show "8"

example 2:
當server start 時自動connect clients (example: 8個online), use TCP
30秒後,第9個client online, now server display "9" (已經做到)

TOP

原帖由 raydan 於 2009-3-9 14:04 發表
i am usingSocket s = wServer.GetSendSocket();
if(s.isConnected() && !s.isInputShutdown() && !s.isOutputShutdown())
{
// connection valid
}當個connection disconnect, 都係valid connection, but client si ...


你或者要考慮下令你個application 唔需要咁快知道斷線。
因為如果你個application 要一秒鐘check 10次先滿足到你既要求,咁個network 稍稍有擠塞就KO 你既application.

TOP

原帖由 raydan 於 2009-3-9 14:54 發表
我個program 是這樣的

1 個server program, 10 個client

example 1:
當server start 時自動connect clients (example: 8個online), use TCP
server display "8", when 1 client disconnect, client 不會send "shut ...


你真係要去到0.1秒準確度嗎?
你可以考慮令client keep 住send heartbeat packet 俾server. Server 為每個client set 一個TimerTask (重點! 留意Timer 同TimerTask 既分別) , 當server 收到來自一個client 既packet, 就reset 相關既TimerTask, 如果某個TimerTask 夠鐘,即係話有一個相關既Client 出左問題。
呢種做法係server 係完全唔需要做polling, 亦唔需要做額外既Thread, 所有野都係Timer-based.

TOP