2

I have a web application (.war file) running under Tomcat. It is a proxy between my c++ client/server applications. Once HTTP request (from the client) is received by web application, it propagates request to the server and sends response back to the client once it is ready. Server may need long time before it produces some data to send to the client (also it can send data by chunks with really long delays etc.).

My question is. Is there a way to detect client disconnect before the time server has something ready to be written to the output stream? In case if server writes something after client's disconnect, obviously I will get an exception and can handle it properly. But my goal is to avoid waiting for the server to produce some data to write, to find client disconnect.

I believe this is a common issue people face and there should be a graceful solution for this. Can you advise?

5
  • Hi EJP, thanks for looking into this. Actually I saw the question you mentioned above while marking my question as a duplicate. It seemed to me too weird to be true :) I can't believe that this kind of common task wasn't considered and handled before. While investigating this question I found that some evening mechanisms are implemented in Spring framework. What about TcpConnectionCloseEvent events in Spring? Is it possible to get notifications from spring when Tomcat closes socket connected to the client when it disconnects ? Commented Aug 21, 2017 at 8:25
  • That's how TCP is. No question of whether it was 'considered'. TCP/IP was designed to survive a nuclear war. It doesn't deliver 'connection dropped' events to upper layers, as there is no such event unless detected during a send. So upper layers cannot deliver it either. Commented Aug 21, 2017 at 9:06
  • 1
    My question is not about tcp/ip. Websockets are also implemented on top of the same tcp connections, but disconnected clients can be detected in case of websckets. I don't see any reason why it is not possible for HTTP. Any documentation ? Commented Aug 21, 2017 at 10:36
  • Websockets rely on the same mechanism as TCP/IP, and so does HTTP. There is no superimposed protocol for this in either case. Commented Aug 21, 2017 at 16:33
  • 1
    Then how do you explain existence of org.springframework.web.socket.handler.BinaryWebSocketHandler class in spring? As you can see from documentation when client disconnect happens afterConnectionClosed method is invoked for websockets. This way one will be notified when client disconnect happens. I don't see any reason why this is not possible for HTTP. Commented Aug 21, 2017 at 19:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.