Closing BufferedWriter Stream closes Socket

Discussion in 'Java' started by Brett Everton, Jun 3, 2004.

  1. Hello,

    I have a simple client server application, where the server creates a
    socket and listens for a client to connect. Once connected, the server
    receives communication from the client via

    socketConnection = socketServer.accept();

    dataIn = new DataInputStream(socketConnection.getInputStream());

    and the server sends out data to the client with

    bufferOut = new BufferedWriter(new
    OutputStreamWriter(socketConnection.getOutputStream()));

    My problem is the client does not detect the end of the stream with

    while ((line = bufferIn.readLine()) != null) {

    I have read that to cause an EOF in a stream the stream must be
    closed, flushing the stream etc is not enough.

    My problem is that when i close the stream ( bufferOut ) on the server
    side, the client detects the end of the stream but the
    socketConnection closes also on the server side.

    Can someone explain why I am losing my socket connection just by
    closing my bufferedwriter stream? Or let me know a better way to go
    about it.

    Thanks in advance
    Brett
    Brett Everton, Jun 3, 2004
    #1
    1. Advertising

  2. On 3 Jun 2004 10:41:43 -0700, Brett Everton wrote:
    > My problem is the client does not detect the end of the stream with
    >
    > while ((line = bufferIn.readLine()) != null) {
    >
    > I have read that to cause an EOF in a stream the stream must be
    > closed, flushing the stream etc is not enough.


    Whenever you close any stream, all underlying streams and the socket
    (or file, or whatever) are also closed. You can't do anything about
    that.

    If you want to indicate the end of a "message" without closing the
    stream, you need to look for something other than EOF at the receiving
    end.

    Your application needs a clearer definition of a message. Typically
    messages are sent using some variation of the following:

    - send a special delimiter after each message. The delimiter can't
    occur unescaped within the message itself
    - send the message length, followed by the message contents

    Since you seem to be using a text-based protocol, a suitable delimiter
    might be something like a newline, or an empty line if your messages
    can be more than one line.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jun 3, 2004
    #2
    1. Advertising

  3. Thank you for you help

    I ended up sending '-1' at the end of the stream on the server side
    and instead of using

    String line;
    while ((line = bufferIn.readLine()) != null) {

    on the client side. I used

    line = bufferIn.readLine();
    while (!line.matches("-1")) { // determine if end of stream
    line = bufferIn.readLine();

    This seems to work well.
    Brett Everton, Jun 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Novice
    Replies:
    1
    Views:
    1,124
    Mike Schilling
    Jul 19, 2003
  2. Jean-Benoit MORLA
    Replies:
    3
    Views:
    9,729
    Jean-Benoit MORLA
    Jul 19, 2004
  3. KevinSimonson
    Replies:
    4
    Views:
    964
    Arne Vajhøj
    Oct 1, 2011
  4. Jan Burse
    Replies:
    3
    Views:
    326
    Tom Anderson
    Jan 8, 2012
  5. karthi
    Replies:
    0
    Views:
    114
    karthi
    Jan 31, 2006
Loading...

Share This Page