I have a midlet that is extensively connecting to the network and data is being sent and received. Instead of establishing a network connection for every request, I was thinking of reusing a connection.
By reusing I mean that I create a connection and then I don�t close it. I keep it alive till the user exits. Using one connection I should be able to send/receive data multiple times.
I did try this out. But I get the following message when the request is being sent for the second time: Write attempted after request finished.
Thanks Mark. Is KEEP_ALIVE a property ? And where do I set it.I did not find anything in the API.
Yuri Magrisso
Ranch Hand
Joined: Sep 13, 2003
Posts: 58
posted
0
I once tried to do that with HttpConnection and I didn't succeed even if I set connection type to keep-alive (httpConnection.setProperty("Connection-Type", "keep-alive"). What I noticed was that the data was not sent to the server until I call HttpConnection.getResponse() or HttpConnection.openInputStream() and once I call any of those I can't write data anymore. It looks like HttpConnection is made to work in request/response type of communication.. This probably depends on the specific phone too (its KVM) - I was working with Motorola i85 then.
With SocketConnection that works without problem, but you either have to implement HTTP by yourself on the client, or write a custom TCP/IP server.
Regards, Yuri
Danish Shaukat
Ranch Hand
Joined: Nov 16, 1999
Posts: 337
posted
0
Found the following replies to a similar question on Sun's discussion forum:
You have to open a new connection for every new HTTP request. Since HTTP is strongly request-response based, you cant write anything to the output stream after you have opened the input stream (wrote request, read response and finish).
You cant reuse the existing connection in HTTP. Of course a socket connection can do this ;-)
hth
-Kay
The HttpConnection object is a "use and throw" object. It works for one sending of the HTTP request, and then receiving the response. That is why once you've opened the input stream you can't write to the output stream. It's already been flushed and closed. But the phone will keep your GPRS connection open. So you can send more requests without the need to to reopen the GPRS link.