×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Munich, Germany
    Posts
    76

    InputStream.read() hangs

    Hi,

    I'm testing my Java application on several phone types.

    It works on 7210 with FW 3.09 but not an FW 4.18 and also not on 3510i with FW 3.40.
    It seems that InputStream.read() hangs and doesn't return. I'm doing communication in a background thread.

    Any ideas?

    Regards,
    Juergen

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Munich, Germany
    Posts
    76

    Sorry, forgot...

    I'm trying to download via HTTP from a server where this read() hanging occurs.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    3
    Hi,
    I've posted yesterday a similar problem for the 6310i emulator coming with the Nokia Developer's Suite. I found that InputStream.read(array, offset, length) works and returns -1 if the end is reached, so I changed my method to read in this way:

    byte[] downloaded = null;
    int downloadedCount = 0;
    InputStream is = null;
    HttpConnection con = null;
    try {
    con = (HttpConnection) Connector.open(url);
    is = con.openInputStream();
    //get the content length if present
    int contentLength = (int) con.getLength();
    if (contentLength > 0) {
    downloaded = new byte[contentLength];
    while (contentLength > 0) {
    int c = is.read(downloaded, downloadedCount, contentLength);
    if (c > 0) {
    downloadedCount += c;
    contentLength -= c;
    } else if (c < 0){
    break;
    }
    }
    } else {
    downloaded = new byte[1024];
    while (true) {
    if (downloaded.length < (downloadedCount + 256)) {
    //needs resize
    byte[] temp = downloaded;
    downloaded = new byte[downloaded.length + 1024];
    System.arraycopy(temp, 0, downloaded, 0, temp.length);
    temp = null;
    System.gc();
    }
    int c = is.read(downloaded, downloadedCount, 256);
    if (c > 0) {
    downloadedCount += c;
    } else if (c < 0) {
    break;
    }
    }

    }
    }

    Please write me at dragozov@yahoo.com, if this works on the phones you test as I try to make my MIDlet work on as much phones as possible.

    Best regards,
    Plamen

  4. #4
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Munich, Germany
    Posts
    76

    InputStream.read() hangs

    Hi,

    my problem is different:

    InputStream.read() hangs and _never_ comes back. Not with -1 neither with another number.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    3
    Mine was the same. After reading some bytes, the method got blocked and never returned. That's why I used read(byte[], int, int) in the way I wrote in the previous post and it worked for the model I tested.

  6. #6
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Munich, Germany
    Posts
    76

    InputStream.read() hangs

    I also tried reading single bytes using read(). But even reading the first byte hangs.

  7. #7
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Munich, Germany
    Posts
    76

    InputStream.read() hangs

    I've played around with this and it seems that if the HTTP download is made in another thread (not in the "main" thread), it happens that InputStream.read() hangs. I've moved the download function to the main thread and start it via Display.callSerially() and now it works.

    Of course it blocks the user input during the download which is not very pleasant :-((

  8. #8
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Munich, Germany
    Posts
    76

    What about an "official" statement from Nokia?

    Maybe someone from Nokia can at least confirm that there _is_ a known problem with multithreading and HTTP download?

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    1

    same problem

    I am having the same problem as well. The first call to read() blocks until a timeout. I've tried both having the read occur on the main thread and on a second thread. Both behave the same way.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    Hey! I do have this problem too!
    Can't find what to do about it...
    I open HttpConnection with request method GET. After calling getResponseCode() I can read all the header fields and see right data Length(). But the very first read() hangs.
    Can it be because of closing connection from server side before reading ?
    Firmware version is 4.18.

    Can Nokia help ?

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    37

    7210 connection thread hangs

    Hey ! i have a similar problem too.

    i haven't got down to exactly which line it's hanging on yet, but seems to be related to;

    1) connection on a new thread
    2) an HTTP post to server
    3) using input stream from HttpConnnection.openDataInputStream();
    4) a relatively complicated series of readXXXX() calls

    suffice to say it works on all sorts of phones, including 6100s, 60series and emulators (for what they're worth). also, annoyingly, other downloads in same app work fine on 7210, steps 1-3 the same, just with slightly less complicated sequence of readXXXX() calls in step 4. urgh!

    another feature of our problem i've noticed, when running our app, is that if you leave the application running after hitting this problem for a while after connection attempted, whole app seizes up, and then trying to "exit app" from our own function (which cleans up then calls notifyDestroyed()) it kind of half closes and then you need to kill the remnants of the app with the HANG UP red button.

    so, i see a lot of posts with various connection difficulties on various phones, and a lot of black magic workarounds, but could some body point us to the definitive list of problems and how to work around? HEY NOKIA!!

    thanks in advance,
    cheers, dan.

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    1
    Ok, I known this thread is getting pretty old. But as long as I cannot see a workaround posted or any other useful info:

    I have the same problem as has been identified in this thread (in short, read() hangs and won't ever return when the network code is run in a separate thread).

    Strangely, the following hack turns out to do wonders:

    public void run()
    {
    try {
    Thread.sleep(1000);
    } catch ( Exception e ) {}

    // Communication code goes here.
    }

    In other words, insert a delay of one second or so before your network code kicks in.

    It's ugly but it works.

    I have tried several sleep values. 1-100 fails, while >= 500 seems to work ok.

    This suggests that the problem could be related to some synchronization issues, but I'm really not sure how to interpret this.

    Any ideas?

    Arne.

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    8

    A possible workaround

    (that seems to work for me) - is to always use the same thread for HTTP access - ie. don't spawn a new thread for a new request, but reuse one thread each time.

    I suspect the bug is in synchronization, and only happens if several threads enter the network stack (even if at distinct times - its like there are some locks attached to the thread even after it closes the HttpConnection). Possibly the bug only shows up if a thread is GC'ed after it performed HTTP requests (another theory to test).

    Anyway by using a single thread I can reliably get runs of 100 HTTP requests without hanging... Wish Series60's were so SLOOOOWWWWW though (about 8 seconds round trip - other phones can manage 3.3 seconds)

  14. #14
    Registered User
    Join Date
    Jul 2004
    Posts
    1

    InputStream.read() & 6600 Firmware version

    Hello,

    I'm getting the same issue with the read() method of InputStream or DataInputStream classes : it hangs.
    The thread manages to connect to the given url, sends data and then stay locked on the read() method. When the connection is closed, it throws an exception (symbian error -36 i.e. disconnected).
    I noticed it only happens since i updated my 6600 firmware from 3.42.1 to 4.09.1.
    Here is a simple code that works fine with the old firmware but doesn't with the newest.
    If someone has any idea...

    thx,
    regards,

    ludo.

    PS : Of course, there is another thread running on another phone that accept the connection, read data sent by the following thread and then send data...

    //--------------------------------------------------------------------------------
    import java.io.*;
    import javax.microedition.io.*;
    import javax.bluetooth.*;


    public class RemoteConnection extends Thread {

    private String url;
    private StreamConnection remoteCon;
    private DataInputStream dataIn;
    private DataOutputStream dataOut;

    private String str1 = "String1";
    private String str2 = "String2";
    private String str3 = "String3";

    private byte[] dataBuffer;


    public RemoteConnection(String connURL)
    {
    url = connURL;
    dataBuffer = new byte[200];
    }


    public void run()
    {
    BT.init();

    try
    {
    remoteCon = (StreamConnection) Connector.open(url);

    if (remoteCon != null)
    {
    Main.debug("RemoteConnection : Connected to " + RemoteDevice.getRemoteDevice(remoteCon).getFriendlyName(false));

    try
    {
    dataIn = remoteCon.openDataInputStream();
    }catch (Exception e)
    {
    Main.debug("dataIn open Error...\n >" + e.getClass().getName() + "\n >" + e.getMessage());
    }

    try
    {
    dataOut = remoteCon.openDataOutputStream();
    }catch(IOException e)
    {
    Main.debug("dataOut open Error...\n >" + e.getClass().getName() + "\n >" + e.getMessage());
    }

    dataOut.write(str1.getBytes());
    dataOut.flush();
    Main.debug("STR1 SENT");

    dataOut.write(str2.get(false));
    dataOut.flush();
    Main.debug("STR2 SENT");

    int nbBytes = dataIn.read(dataBuffer);
    Main.debug("==> READ " + nbBytes + "bytes :\n >" + new String(dataBuffer));

    nbBytes = dataIn.read(dataBuffer);
    Main.debug("==> READ " + nbBytes + "bytes :\n >" + new String(dataBuffer));

    dataOut.write(STR3.getBytes());
    dataOut.flush();
    }
    }
    catch(Exception e)
    {
    Main.debug("RemoteConnection Exception :\n >" + e.getClass().getName() + "\n >" + e.getMessage());
    }
    die();
    Main.debug("RemoteConnection : CLOSED");
    }




    public void die(){
    setPriority(10);

    try
    {
    dataIn.close();
    dataIn = null;
    }catch(Exception e)
    {
    Main.debug("dataIn close error :\n >" + e.getClass().getName() + "\n >" + e.getMessage() + " " + hashCode());
    }

    try
    {
    dataOut.close();
    dataOut = null;
    }catch(Exception e)
    {
    Main.debug("dataOut close error :\n >" + e.getClass().getName() + "\n >" + e.getMessage() + " " + hashCode());
    }

    try
    {
    remoteCon.close();
    remoteCon = null;
    }catch(Exception e)
    {
    Main.debug("RemCon close :\n >" + e.getClass().getName() + "\n >" + e.getMessage());
    }

    Main.debug("RemCon : DEAD");
    }


    }
    //--------------------------------------------------------------------------------

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    4
    Arne. Thanks.

    Yr. approach really worked. I met the issue was that my midlet can run on Nokia 7210 and 7250, but can't run on Nokia 6610. On 6610, once running httpConnection, the read() function hanged.

    so i added the codes as u described and tried several times. i found Thread.sleep(1000) didn't work, but Thread.sleep(600) worked.

    what a mess! "It's ugly but it works. ", YES, ^_^, i solved this problem!

    SAM

Posting Permissions

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