1

I coded a small server with NIO while walking normally except that the server can not make broadcasts at certain times to my clients because of the closure of the channel. Is there a solution against this problem? I make http requests via the browser.

Thank you for your help. Cordially.

12
  • The simple answer is yes, you can keep the socket open. I don't know why you feel you need to close it. BTW nice pic. ;) Commented Jun 1, 2013 at 21:51
  • Thank you for your response, I wish to make HTTP requests, if I close the socket I get my answer if the application runs in a loop so I'm forced to close the socket. The problem is that after the server can not make broadcasts with new updated information to my clients Commented Jun 1, 2013 at 21:58
  • 1
    Your browser must support persisted HTTP 1.1 connections. If it does you can keep the connection open and the browser can reuse it many times. If you browser is not using these persisted connection you have no choice but to close the connection as this is what the browser expects. See en.wikipedia.org/wiki/HTTP_persistent_connection for more details. Commented Jun 1, 2013 at 22:01
  • 1
    Merci pour votre réponse mon navigateur gère les connexions persistantes je vous met le code qui écrit le Str au channel mon code actuel débutant avec NIO, j'ai peut-être glissé une erreur. Commented Jun 1, 2013 at 22:16
  • private static void channelWrite(SocketChannel channel, ByteBuffer writeBuffer) { long nbytes = 0; long toWrite = writeBuffer.remaining(); try { while (writeBuffer.hasRemaining()) { channel.write(writeBuffer); } channel.close(); } catch (ClosedChannelException cce) { infos("problème"); } catch (Exception e) { } // get ready for another write if needed writeBuffer.rewind(); } Commented Jun 1, 2013 at 22:18

1 Answer 1

1

You can take a look into Comet technology. Of course you browser should support Http1.1 keep-alive

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.