Results 1 to 2 of 2
  1. #1
    TacoManStan is offline Member
    Join Date
    Sep 2011
    Rep Power

    Default How often should a stream be closed?

    So I am very new to the whole world of servers and streams, and I am wondering, how often should I stream be closed? Now I'll elaborate because that is such an incredibly vague and unhelpful question.

    The tutorial that I found on the Oracle website has a very simple socket connection set up. I have the connection set up through localhost as of now to keep it simple. The server I am going to be using will potentially have a very large number of clients, for now I will say 100. I need the connections to be as efficient as possible. I have the server listening for connections in a while loop, like this.

    Java Code:
    while (listening) {
         new TAiServerThread(serverSocket.accept()).start();
    This has been tested and works properly. It starts a new thread which takes care of that particular client connection request. My question is, once a client is connected, should it stay connected until its termination? Or should the client connect to the server whenever it wants to retrieve even the smallest amounts of information? In my program, a client would potentially need to receive information from the server multiple times per second. I don't know if opening and then closing a connection to a server that many times would be a good idea. It seems like it would create a lot of unnecessary work, and that leaving it open would be a lot more efficient. But again, I don't know enough about sockets to know if that's true.

    If this matters at all, the server would be sending a large variety of information through an object stream.

    EDIT: Another question would be, if I shouldn't open a new connection multiple times a second, I should I go about keeping the connection open, and continuously get information?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: How often should a stream be closed?

    It's all in the protocol: to get the circus up and running, a client wants to establish a connection and (hopefully) the server accept()s this connection. If the client wants to know just one thing from the server, it asks the server for this information, the server sends it, the client receives it and closes the connection. Otherwise, the client doesn't close the connection and asks the server for more information etc. etc. The client should anouce that it wants to close the connection (you don't want to keep the server waiting for nothing) and finally close the connection.

    It's all up to the protocol (that you define) how the client communicates with a server. Both parties should agree with the protocol: either it's a one shot information exchange or multiple queries can be send from a client to the server. For the second alternative, both parties should agree when a connection should be closed an which peer actually closes the connection (either the client or the server); it's all in the protocol.

    b.t.w. you can only close a Stream once.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 9
    Last Post: 01-30-2013, 07:17 PM
  2. Should this input stream be closed?
    By barock in forum Advanced Java
    Replies: 8
    Last Post: 06-10-2011, 08:11 PM
  3. Replies: 0
    Last Post: 01-21-2011, 01:43 PM
  4. Stream closed on a ClassLoader input
    By RaistlinMajeren in forum Advanced Java
    Replies: 15
    Last Post: 06-03-2010, 08:18 AM
  5. Replies: 1
    Last Post: 01-31-2010, 04:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts