• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reusing Network Connection

 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both HttpConnections and SocketConnections require that you set the Connection to KEEP_ALIVE.

Mark
 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark.
Is KEEP_ALIVE a property ? And where do I set it.I did not find anything in the API.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic