Ðề: Hỏi : Lập trình tcp socket trong java - Bắt exception khi gửi dữ liệu đến server, nếu lúc gửi mà văng exception thì báo là bị disconnect (tất nhiên là có thể có nhiều nguyên nhân gây ra exception, nhưng mà gửi dữ liệu đi không được thì cũng chẳng khác gì bị ngắt kết nối).
- Dùng heartbeat: Cứ sau một khoảng thời
gian (timeout) nhất định, nếu giữa server và client không có trao đổi nào, server sẽ thử gửi một gói tin (heartbeat) đến client. Nếu nhận được gói tin này, client phải trả lời lại server. Làm như vậy, bên client sẽ xem như server ngắt kết nối nếu timeout mà không nhận được heartbeat nào từ server. Server cũng sẽ xem như kết nối đến client bị ngắt nếu sau khi gửi heartbeat, không thấy client trả lời lại.
Ðề: Hỏi : Lập trình tcp socket trong java Hi cảm ơn bạn đã giúp đỡ. Trường hợp của mình đang gặp là server sử dụng socket.close(), làm sao bên client biết được. Bởi vì TCP là kết nối có kiểm tra quá trình kết nên ví dụ như khi server sử dụng lệnh socket.close() thì trong cmd sử dụng lệnh netstat -na thì thấy port client là close_wait => làm sao để biết trạng thái này trong
kết nối. Còn khi server bi shutdown ==> không thấy port kết nối của client tới đó. Nhưng làm sao ta biết được việc này ??? trong java
Ðề: Hỏi : Lập trình tcp socket trong java Qua kinh nghiệp sử dụng thì mình thấy có vẻ Java API hỗ trợ việc close TCP connection không được tốt lắm. Nếu trên server chỉ gọi socket.close() thì trên client không có cách nào nhận biết điều này cả (ngoài việc bị văng lỗi khi cố gửi packet tới server). Nếu việc ngắt kết nối là có chủ đích (không phải do lỗi) thì mình thường
tự gửi 1 gói tin (thay cho gói FIN của TCP) yêu cầu client close socket rồi mới gọi socket.close() từ phía server. Trên trang Stackoverflow cũng đã có câu hỏi tuơng tự như của bạn: Why is it impossible, without attempting I/O, to detect that TCP socket was gracefully closed by peer? nhưng có vẻ là không có câu trả
lời nào thỏa đáng cả.
Ðề: Hỏi : Lập trình tcp socket trong java - Bắt exception khi gửi dữ liệu đến server, nếu lúc gửi mà văng exception thì báo là bị disconnect (tất nhiên là có thể có nhiều nguyên nhân gây ra exception, nhưng mà gửi dữ liệu đi không được thì cũng chẳng khác gì bị ngắt kết nối).
- Dùng heartbeat: Cứ sau một
khoảng thời gian (timeout) nhất định, nếu giữa server và client không có trao đổi nào, server sẽ thử gửi một gói tin (heartbeat) đến client. Nếu nhận được gói tin này, client phải trả lời lại server. Làm như vậy, bên client sẽ xem như server ngắt kết nối nếu timeout mà không nhận được heartbeat nào từ server. Server cũng sẽ xem như kết nối đến client bị ngắt nếu sau khi gửi heartbeat, không thấy client trả lời lại.
cho em hỏi là anh đã thực thi theo heartbeat chưa
ạ, em cũng đang vướng mắc như này mà chưa thể nào giải quyết được, mong anh chỉ giáo JoeThành viên VIP
cho em hỏi là anh đã thực thi theo heartbeat chưa ạ, em cũng đang vướng mắc như này mà chưa thể nào giải quyết được, mong anh chỉ giáo
Forget the "heartbeat" and all the bullshits. If a server gets down the client hangs. No way to work around. A client always reads or writes (sends) something to a server and gets: a) a response if the server and its
service is available (i.e. online), or b) an exception (write/read error because of socket closed, etc.) when the server was down or its service wasn't available. TCP/UDP-IP communication is similar to your cellular communication. If a sender of Viettel gets down your cell phone gets lost without any forewarning.
Forget the "heartbeat" and all the bullshits. If a server gets down the client hangs. No way to work around. A client always reads or writes (sends) something to a server and gets: a) a response if the server and its service is available (i.e. online), or b) an exception (write/read error because of socket closed, etc.) when the server was down or its service
wasn't available. TCP/UDP-IP communication is similar to your cellular communication. If a sender of Viettel gets down your cell phone gets lost without any forewarning.
Xin lỗi, anh có thể nói rõ hơn được không ạ. Em cảm ơn JoeThành viên VIP
Xin lỗi, anh có thể nói rõ hơn được không ạ. Em cảm ơn
Sorry, I can only explain in English. Socket communication is similar to Cellular communication. The connection between two partners are loose and lax. It's because of the laws of physics and of the nature. That is: 1) How can a partner know that the other suddenly skips or shutdowns the
dialog ? Answer: He/she gets a wrong or disconnected noise. 2) How can they know that the physical medium is suddenly interrupted by road work or whatever ? Answer: They mistrust each other for the silent (dis)connection. 3) How can they know that their communication is interfered by electric noises or interference ? Answer: They mistrust each other for the noisy connection. ... They are the reasons why TCP/UDP-IP or Cellular Communication is loose and lax. You can only check for
your communication when you (or Client) and your partner (or server) agree on something called "synchronization". If this "something" is interrupted then you know "shit" happens upon your communication. In JAVA Client-Server: PHP: // server's service or on the Client site
try {
...
} catch (Exception e) { // any kind of exception = disruption of communication.
// do the panic rescue
}
any kind of quests like isClosed() or isWhatever() refers only the the local state of the socket or the state when socket was opened. None refers to the actual state on the other site. Also, pls. read the APIs
and try to ponder on the description of every method, then you will see "heartbeat" is only one of the bullshits. |