5

I have written my server side code for long polling. I want to write the client program in java. So as per long polling, the client sends a request which is help by the server and the server responds to the request when an event occurs and then client sends a new request.

So the trouble I am facing is with the client side which is to be written in java. After I send the request, how to keep checking in the client side if the server has responded to it or not. At what intervals should I keep sending the request to the server. I am totally confused. I am quite a beginner to Web technologies. I tried googling about this but all the examples are based on client side being a java script or JSP. Could anyone give a link to a proper tutorial with client side being a java HTTp application or provide an example on this (ie to achieve long polling).

Ashwin Surana
  • 826
  • 3
  • 10
  • 33

1 Answers1

7

The call to HTTPURLConnection's getInputStream gives back a blocking stream. Calling a read on this stream will block the thread till data is available from the server, you need not keep polling for data.

Call the read in a separate thread and keep the HTTPURLConnection in scope without closing the connection. This should get you back the data nwhen available. Once you receive a 200OK from the server, send back another request on the same connection to keep it open. (This is if you have not implemented a request timeout.)

Raji
  • 527
  • 3
  • 6
  • okay one more clarification . How to resend a request on the same connection :| . I have been trying this for a long time. – Ashwin Surana Jul 24 '13 at 06:06
  • Hope your server is not closing the connection once the response is sent? If it is then U will have to connect again. What is the exception you are getting? – Raji Jul 24 '13 at 12:47
  • okay here is what I have Done. I am not reading the input stream on a separate thread as of now. for testing its all on the main thread. its just that everything is inside a loop. I start a connection and then I start my loop. I check the response code if its 200 i read. if its something else i have to resend the request. instead of resending a request it goes in a infinite loop :| . – Ashwin Surana Jul 24 '13 at 14:23
  • check out the sample code I have at http://rsankarx.wordpress.com/2013/07/25/http_server_long_polling/. Not sure what you might be doing wrong unless I see the code. – Raji Jul 25 '13 at 01:40
  • Below is the link to my other question with the code. Though I got my program running :) by taking the advice of the first two commentators, it would be nice of you to suggest your opinion on the code or a better method to achieve it :). http://stackoverflow.com/questions/17826107/httpclient-4-2-5-how-to-resend-a-get-request – Ashwin Surana Jul 25 '13 at 17:06
  • @Raji "Hope your server is not closing the connection once the response is sent?" - if the server does not close the connection, then why does the client need to do long poll ? – mangusta Nov 17 '14 at 11:08
  • it connects once, and server pushes him the messages. or maybe i got something wrong? – mangusta Nov 17 '14 at 11:09
  • How is it implement in Android? – Shahbaz Ahmed Nov 05 '18 at 15:16