Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java Micro Edition and the fly likes Reusing Network Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Reply Bookmark "Reusing Network Connection" Watch "Reusing Network Connection" New topic
Author

Reusing Network Connection

Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 337
Hi all,

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.

Is it possible to reuse a connection?

Danish
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17234
    
    1

Both HttpConnections and SocketConnections require that you set the Connection to KEEP_ALIVE.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 337
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
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
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.

-Shmoove
[ June 01, 2005: Message edited by: Danish Shaukat ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reusing Network Connection
 
Similar Threads
Chunked transfer-encoding problem
Posting file to non-existing URL from Mac
What is the maximum amount of data that can be sent ina XML request
Using HttpConnection in MIDP
IIOP Protocol