Question

I have a problem with a BufferedOutputStream. I want to send a kml file from an Android device to a java server through a socket connection. (The connection is ok, i am already able to exchange data with a PrintWriter in an other part of my program)

To send my kml file, I fill the buffer. But when i flush() it, nothing happen.

        int lu = inFile.read();
        while(lu != -1){
            out.write(lu);
            lu = inFile.read();
        }
        out.flush();
        inFile.close();

inFile is my stream used to read the kml file out is my BufferedOutputStream using the OutputStream of my socket

I don't close my out object but i don't want to, i don't use it just once. And this is the problem... The close() method send the buffer's data but close the socket too. The flush() method does not send the buffer's data.

I want to flush the buffer without closing my socket.

I also tried to use mySocket.shutdownOutput();

        int lu = inFile.read();
        while(lu != -1){
            out.write(lu);
            lu = inFile.read();
        }
        out.flush();
        mySocket.shutdownOutput();
        inFile.close();

This method close my stream and keep my socket open, that's what i want. But when i try to open a new output stream, the Exception java.net.SocketException: Socket output is shutdown

So, how to flush my buffer without closing my sokcet are being unable to open a new output stream ?

Was it helpful?

Solution

Socket.close() and Socket.shutdownOutput() both send an EOS to the peer, on which he should close the socket, and after which you can no longer write to the socket, because you've closed it in that direction.

So if you need to continue writing to the socket you cannot use either of these methods.

Probably what you are searching for is a way to delimit application protocol messages. There are at least three techniques:

  1. Send a length word prior to each message.
  2. Send an out-of-band delimiter after each message, i.e. a byte or byte sequence that cannot occur in a message. The STX/ETX protocol, with escapes, is an example of this.
  3. Use a self-describing message format such as Object Serialization or XML. STX/ETX is also an example of this.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top