Results 1 to 7 of 7
  1. #1
    eyal is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default Writing to Socket outputStream w/o closing it

    Hi,

    I'd like to write some messages to the server.
    Each time, for the tramsmitting only, I'm closing the outputStream and reopen it when I have to send the next message.

    Java Code:
    		os.write(msgBytes);
    		os.write("\r\n".getBytes());
    		os.flush();
    		os.close();
    Is it possible to keep the outputStream, os, open and still able to send the message?

    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    have you tried it?
    What happens?

  3. #3
    eyal is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default The problem has been solved

    I have found it in the client's code.
    I have read the data from server:
    Java Code:
     (count = inputStream.read(b) ) > -1
    which hold the data till server socket closed.
    This couses to the problem.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    This couses to the problem.
    Not sure what this means. Could you explain?

    Have you answered your question?

  5. #5
    eyal is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default Yes. sure

    While reading the data with
    Java Code:
     InputStraem.read(buffer)
    , you can accomulate the incoming data and have it available for a use.
    In my code, I have read the data but process it after the reading returns with -1, which means "end of streaming". Actually, -1 means end of connection.
    For finding the end of streaming you can use eather
    Java Code:
     inputStream.available() == 0
    or reading the incoming data till reading some predifined set of bytes which are representing "End of Streaming".

  6. #6
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Actually, -1 means end of connection.
    -1 has got nothing to with connection, it simply denotes the end of stream.

    InputStream (Java 2 Platform SE v1.4.2))
    Swastik

  7. #7
    eyal is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default Ok.

    I meant to connection in context of InputStream / OutputStraem existeance/

    As long as OutputStraem, of the sender, is not closed the the receiver's inputStraem.read(buffer) will never read -1, even if Sender has nothing more to send.
    Last edited by eyal; 05-17-2010 at 04:31 PM.

Similar Threads

  1. Problems writing to an outputstream
    By higashij in forum Java Servlet
    Replies: 1
    Last Post: 09-12-2009, 07:15 PM
  2. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 07:02 PM
  3. Outputstream not streaming.
    By dantestein in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 07:36 AM
  4. interrupting urlconnection's outputstream
    By zynasis in forum Networking
    Replies: 5
    Last Post: 10-02-2008, 12:49 AM
  5. HashMap to share OutputStream
    By gabriel in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 05:47 PM

Posting Permissions

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