×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Mar 2003
    Location
    Copenhagen, Denmark
    Posts
    18

    Keep HTTP connection alive on 7210

    Hi guys,

    Does anyone out there have a working example of how to keep an HTTP connection open during multiple requests and responses?

    The second time that I write to the outputstream, no bytes is send to the server (yes I remember to flush :-)

    Maybe I've got it all wrong, but unfortunately it seems that the 7210 does not support keeping connections alive. I must get a new connection in order to perform a new post.

    Here is a snapshot of the sourcecode from my test client, that polls the server each 5th second:

    // Open connection
    // ======================================
    try {
    connection = (HttpConnection) Connector.open("http://127.0.0.1:8020", Connector.READ_WRITE);
    connection.setRequestMethod(HttpConnection.POST);
    }
    catch(Exception e) {
    Alert alert = new Alert("Network error", e.getMessage(),
    null, AlertType.ERROR);
    Display.getDisplay(this).setCurrent(alert);
    e.printStackTrace();
    }
    // --------------------------------------

    boolean openStreams = true;

    while(true) {
    try {

    // Do post
    if(openStreams){
    out = connection.openDataOutputStream();
    }
    out.write(new String("Test post!&quot.getBytes());
    out.flush();


    // Read HttpResponse content
    if(openStreams){
    in = connection.openInputStream();
    }
    int len = (int) connection.getLength();
    in.read(buffer, 0, len);

    openStreams = false;
    }
    catch(Exception e) {

    Alert alert = new Alert("Network error", e.getMessage(),
    null, AlertType.ERROR);
    Display.getDisplay(this).setCurrent(alert);
    e.printStackTrace();
    }

    try {
    Thread.sleep(5000);
    }
    catch(InterruptedException ie) {
    }
    }

  2. #2
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    curious. Did you ever find a solution to that? I'm having the same problem now.

    Many thanks!

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    HttpConnections are not reusable. If you read the javadocs you'll see that the object has a very specific set of states (setup, connected, and done), and certain methods will change the state of the connection. You can't revert back to a state after it is changed so you can't use the same object twice.

    But, since opening the GPRS connection is usually the most expensive part of the networking, most phones will keep the connection alive for you in the background. Notice how the little globe icon stays there even after you close all the connections? So in effect, in order to keep the connection alive you have to send data over it every x seconds (30 seconds seems to be the standard), before the actual connection (and not the HttpConnection object, they are not really the same thing) times out.

    shmoove

  4. #4
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    Thanks schmoove for your response.

    I'm not entirely sure it answers my question. I understand they are not reusable, but I don't think that's what I'm doing (i think).

    I'm trying to make multiple post requests (in succession) using the same HttpConnection object before closing it (either through close() or through closing all connected streams, which according to javadoc means I'm still in connected state).

    I suppose I should have made my question more specific. Is it possible make multiple post requests (in succession) to the same page without making the same number of HttpConnections?

    If yes, how? (my code is similar to schm1's below but not working even on emulator) If no, well.. I have my answer.

    Thanks a lot!

  5. #5
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    I reread the docs and saw that it doesn't really mention it, but from my experience calling flush() on an OutputStream will often cause a transition into the closed state (although not a complete one since you still have to close all streams and connections).

    shmoove

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    You cannot keep-alive an HTTP connection, certainly not on a 7210. On devices that have no integral TCP/IP support, the connection is being handled using WAP - WAP is basically connectionless, so there is no connection to keep-alive. The phone sends the request to the WAP gateway, which then executes the entire HTTP transaction itself, then bundles up the response and sends it back to the phone.

    Cheers,
    Graham.

  7. #7
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    Thanks both of you. Really appreciate the help.... really!

    Curious, but what other devices have that functionality of keeping connection alive, and what am I to look for in the device specification to find out? I read through the series 60 specs, and have a vague idea that perhaps it does have integral TCP/IP support, but unsure if I'm picking out the right stuff.

    Yeah.. I know.. n00b question. Sorry!

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    Given that phones are inherently connected devices, you'd think that networking might be one of the easier aspects of MIDP development... but no!

    If you can't live with a transactional, request-response system like basic HTTP, and you need some kind of conversation, then you'll need to use a TCP, socket based connection.

    Series 60s will support this, though older ones might support it badly. S40 DP2s (like the 6230) support sockets too. Most Motorolas support socket connections, as do the newer SonyEricssons.

    Cheers,
    Graham.

  9. #9
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    And if I cannot mention my thanks once too many times, thank you.

    After reading all that I *should* have known beforehand, I feel slightly wiser... very much a fool still, but a wiser one.

    Cheers,
    Lisa

  10. #10
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    There is no way that you should have known such things... I think it's perfectly reasonable to assume that a phone that does HTTP will actually do HTTP.

    The great challenge and "fun" of MIDP development is the slow and painful process of learning how each model of phone fails to do what it's meant to!

    Graham.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×