Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User _ericlo_'s Avatar
    Join Date
    May 2003
    Posts
    8
    Question as titled.
    I am developing a server-client application.
    All http connections are made to run on a background thread.

    Here's a code snip.
    try {
    hc = (HttpConnection) Connector.open(connaddr, Connector.READ_WRITE, true); // hc is of class HttpConnection
    } catch (InterruptedException timeout) {
    System.err.println("Timeout detected");
    } finally {
    hc.close();
    }

    I believe that this code should work fine. But is there a way I can really specify the timeout property? Or it would be great if I can read the timeout property at least.

    Thanks all.

  2. #2
    You may do this.. Start a timertask, which is scheduled after a timeout period. This task merely closes your connection, so if your connection was in a waiting/receiving state, it will merely throw an IOException and exit, as connection has been closed.

    Timer tmr = new Timer();
    tmr.schedule(new ConnectionTimer(), timeout);

    private static class ConnectionTimer extends TimerTask
    {
    public void run()
    {
    conn.close();
    }
    }

  3. #3
    Registered User gtwdaizi's Avatar
    Join Date
    Sep 2007
    Posts
    9
    sorry, i don't think it can work..
    when the programe is blocked in "Connector.open", you call the conn.close, it doesn't work. in my opinion, the only way is to kill the thread. but this way can only be supported in cldc1.1.

  4. #4
    Regular Contributor ravivalecha's Avatar
    Join Date
    May 2008
    Posts
    68
    Try this may this helps you

    Timer timer = new Timer ();
    MyTimerTask mTT = new MyTimerTask ();
    boolean connectionNotEstablished = true;

    private class MyTimerTask extends TimerTask {
    public final void run() {
    // your body goes here
    if (connectionNotEstablished) {
    // close all opened streams and the connection if they are not null...
    callOfflineTask();
    }
    }

    // another Class Extends Thread

    private void makeConnection (String url) throws IOException {
    try {
    // here goes ur connection establishment
    timer.schedule(mTT, timeout) ; // ur desired timeout.
    // and the tasks afterwards..
    setConnectionNotEstablished(false); // if connection establishes.
    // some other tasks.
    // Open Connection
    } catch (Exception e) {
    //Cancel Timer
    throw new IOException(e.getMessage());
    } finally {
    // close all opened streams and the connection if they are not null...
    }
    }

    public void run () {
    try {
    makeConnection (URL);
    }
    catch (IOException ioe) {
    mTT.cancel();
    callOfflineTask();
    }
    }

    Thanks
    Ravi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved