tags:

views:

77

answers:

2

Hi, i want to do a client-server activity like this: 1. first the client sends/writes to output stream 2. the server responses with some data that will be read with input stream 3. after receiving the data, the client sends/writes to output stream again to respond that the data has been received

now, do i have to close the output stream and re-open it again before doing step no.3 ? also if someone could provide me with a snippet, it would be really helpful. thanks

+1  A: 

You could if you want to, but you don't need to. However, you would need to flush() after writing to the OutputStream in case it is buffered.

The problem would be to know when to read. If the number of bytes to be read is available and performance is not an issue, you can use the blocking call readFully. But this is just one possible strategy.

Chris Henry
thanks, but before i get to write to stream for the second time, i got RuntimeException Stream closed exception. i didn't do close()
Sirius
I see from the other comment that you're doing HTTP connection. If so, it makes sense. The browsers will not use the same TCP link to send two different POSTs. So basically once the first HTTP connection is done, it will be closed by the client (causing the server to throw closed stream error when it tries to write to that connection). When the client is sending the second POST, another TCP link is started.
Chris Henry
if so, do you have a solution to this. i'm really stuck, there's got to be the second POST request
Sirius
A: 

Google is your friend. See this.

Chuk Lee
thanks Chuk but i use httpconnection because i'm dealing with PHP script, and i want to send the data via POST. the server side would catch it and then perform some database query which the results are sent back to the client to be read and processed. then the client would send the result of the processing back to the server via POST again.
Sirius