8

I'm implementing a long poll http connection using java servlet.

How can I know that the http client is still active at any instance? Currently, what I do is to write a byte to the output stream and flush data. If there's an IO exception then the client is dead.

But in ASP.NET there is a property, Response.IsClientConnected which can find out if the client is active without writing anything to the output stream.

I want to know how if it is possible to develop in java servlet. I do not want to keep writing data into the http response stream as it may cost network.

Thanks in advance.

Thomas Jung
  • 32,428
  • 9
  • 84
  • 114
PC.
  • 6,870
  • 5
  • 36
  • 71
  • 1
    I wonder how `IsClientConnected` is implemented in ASP.NET, as it's tricky to find out if a connection is still valid in TCP/IP *unless* you actually send some data. – Joachim Sauer Sep 30 '11 at 06:08
  • PHP also has this functionality: [connection_aborted](http://www.php.net/manual/en/function.connection-aborted.php), [Connection handling](http://php.net/manual/en/features.connection-handling.php) – palacsint Sep 30 '11 at 07:09
  • 2
    @Joachim: No actually thats incorrect. cos if i make a raw TCP socket in Java, server immediately comes to know that client is dead without writing any data, and vice-versa. So, basically Java http connection does not expose this event. thats strange. or i assume there may be some king of buffering at http layer. – PC. Sep 30 '11 at 16:23
  • @PC.: how did you test that? And what do you mean by "dead"? Are you talking about actively closing the connection or about "the connection got interrupted because half a continent further a router exploded"? – Joachim Sauer Sep 30 '11 at 17:01
  • 1
    @JoachimSauer I created client-server sockets using java.net.Socket library. both client and server can send/receive messages at the same time. now consider the connections are ideal i.e. server is waiting for client's response and client is waiting for server's response. now i kill the client (by closing the terminal in which client is running). its like lost of network connectivity. server's thread that was waiting for client's response immediately throws IO exception. this is a very basic functionality of a socket. if u've any doubt, i can share u the code. – PC. Sep 30 '11 at 18:39

2 Answers2

3

It will be difficult to achieve that using Servlet APIs. Though the low level Socket APIs provide this functionality (Socket.isConnected() ), but same functionality is not available through any higher level APIs. Not sure if you any compulsions of using Servlet APIs or you can use low level socket APIs.

Santosh
  • 17,667
  • 4
  • 54
  • 79
  • 1
    using low level sockets is also not an option for me cos my clients will act very stingy when i'll ask them to open a port at server firewall. its gonna be http. i've already done it in asp.net. up and workin 100% efficiently since last two days. why isn't it possible in java. (God, i used to hate M.S. do i need to reconsider my thoughts??) – PC. Sep 30 '11 at 16:31
  • @PC, You mentioned _I do not want to keep writing data into the http response stream as it may cost network._ But the moment you write something to a dead client, you face exception and that's where you stop ! I mean there is hardly any network cost involved (in terms of data transfer). – Santosh Oct 01 '11 at 04:37
  • 1
    Its a long pol connection. so its alive for a very long time. Every 5 seconds the server checks if the client is alive by writing 1 byte. Thats around 16 KB data per day, and it will cost a lot when my client is on a mobile network. – PC. Oct 01 '11 at 13:55
  • Never thought of Mobile client. tell me what kind of client you have, a browse or a native application? – Santosh Oct 02 '11 at 06:14
  • 1
    @PC, your case is slightly tricky one and after doing a bit of research, I am sure the standard servlet API will not address your problem. The closest thing which comes to what you need is [Websockets](http://en.wikipedia.org/wiki/WebSocket). Currently there are no standard implementation for this. There is one with [Jetty server](http://blogs.webtide.com/gregw/entry/jetty_websocket_server). I cannot vouch if it address your problem but you can explore this further for you needs. – Santosh Oct 03 '11 at 07:00
-1

Maybe you've taken the wrong approach? HTTP protocol is developed to be used in a request-response style, it is not suited to be used for a long polling. In fact, there should be lowest possible delay before client gets a server response.

The case you've described looks like a job for a good old Socket.

Alex Abdugafarov
  • 6,112
  • 7
  • 35
  • 59
  • 2
    i completely agree with u. and i know that TCP/IP sockets are best solution for this. but its my limitation that i've to work on port 80 because my client socket my reside inside browser for some cases. and i wouldn't have raised this question if this was not working in .net. if .net can do it, then why not java. and also conceptually, http socket internally sits over a tcp/ip socket. its obvious that it can read the status of underlying socket and tell me if the client is alive. but java.net.Socket library designers forgot to implement this i guess – PC. Sep 30 '11 at 18:44