×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Connection set blocking false...

    Hi,

    I've got a problem that inputstream.read(byte[]) is blocking until data is received...

    I was told that there is some sort of setting to set the blocking to false (for the connection) and if you try to read and no data is available it will return -1...

    Anyone know how to set that blocking value to false ?

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Connection set blocking false...

    InputStream.read(byte[]) will block until some data is available. However, it will not necessarily block until all the data you requested (enough to fill the array) is available. This behaviour is not switchable.

    It will return -1 at the end of the stream.

    If you are reading from a socket (if I remember your other posts), then you are not reaching the end of the stream (because the socket in remaining open, and a socket is a never-ending stream). In this case, you either need to know how much data to read, and read that amount, or you need to read until you reach some suitable termination marker.

    You could use available(), but I cannot guarantee that this won't always return 0 on some devices. It is frequently an unreliable method (certainly on other types on InputStream).

    You could consider reading into some kind of queue, and processing the queue on another thread, allowing the thread on which read() is executing to block without problems until more data arrives to add the processing queue. However, you might have problems closing the connection while there is an open stream attached to it that is engaged in a blocking operation.

    Graham.

  3. #3
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: Connection set blocking false...

    Hi, yes i can confirm 100% true what you said about the end of stream in a socket connection because there is no end of stream.

    Quote Originally Posted by grahamhughes View Post
    You could use available(), but I cannot guarantee that this won't always return 0 on some devices. It is frequently an unreliable method (certainly on other types on InputStream).
    The thing is that when i discussed with the web developer he told me that there must be some kind of way to get all data from the stream, just the data... (obviously reading the whole stream together not character by character, because character by character works 100%)

    So the available() if it's working with a particular model (in my case Nokia 6120classic) is it guaranteed that for that particular class (InputStream) on that particular model (Nokia 6120 classic) it will always work ?

    Because in that case i can leave it like that for the time being and when i'll be working with other phones i will test it first on them, then if i'll have a problem i'll make code (reading character by character for certain phones and reading all the characters using the available for other phones...)

    IF it's reliable for a particular model after being tested...

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Connection set blocking false...

    Quote Originally Posted by kurteknikk View Post
    The thing is that when i discussed with the web developer he told me that there must be some kind of way to get all data from the stream, just the data... (obviously reading the whole stream together not character by character, because character by character works 100%)
    That depends what you mean by "all the data". Is "all the data" everything until the end of the stream? Or everything until it blocks? You can read until it blocks, that's not a problem. However, it may block simply because "all the data" has not yet been received. Remember that you can read data faster than it can arrive. And, it arrives in chunks, not a continuous stream.

    Perhaps it would help to know something about the data you are sending. Is it one continuous stream of text? Or is it a sequence of discrete data units (let's call them "fields")?

    Quote Originally Posted by kurteknikk View Post
    So the available() if it's working with a particular model (in my case Nokia 6120classic) is it guaranteed that for that particular class (InputStream) on that particular model (Nokia 6120 classic) it will always work ?
    Remember that you are never using an instance of InputStream. If you were, you could guarantee 100% that available would always return 0, because that is InputStream's specification for available(). InputStream is never the class of the object you are using. The problem is that the object you are using is an instance of a class that is not part of the API per se. It is an implementation-class, and has no specification.

    Yes, it might work on that device in that circumstance.

    Remember that an InputStream acquired from a different source (such as an HttpConnection or FileConnection) might not behave the same way even on the same device. So just because available() works the way you expect on a SocketConnection, does not mean it will in different situations.

    Also, an InputStream acquired from a SocketConnection on a different device might not behave in the same way. There is no requirement for it to do so.

    Quote Originally Posted by kurteknikk View Post
    Because in that case i can leave it like that for the time being and when i'll be working with other phones i will test it first on them, then if i'll have a problem i'll make code (reading character by character for certain phones and reading all the characters using the available for other phones...)
    Yes. However, there are two problems with this approach:

    1. How many devices do you plan to support in the future? There are several thousand different models currently in use. Do you plan to test on each one?

    2. Reading character by character can be very slow, it's very, very inefficient.

    Graham.

  5. #5
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: Connection set blocking false...

    That depends what you mean by "all the data". Is "all the data" everything until the end of the stream? Or everything until it blocks?
    By "all the data" i mean, all the data that is available until it blocks... it doesn't matter if its not all the data, because i'll be checking for data every second so if at the first second half data has been arrived, then the continuation of the data will read the next second so it doesn't matter for me.

    You can read until it blocks, that's not a problem.
    How can i read until it blocks, without knowing the length of the data to be received?

    Perhaps it would help to know something about the data you are sending. Is it one continuous stream of text? Or is it a sequence of discrete data units (let's call them "fields")?
    It is a sequence of discrete data units, and if a "field" hasn't been received all (i.e there is no message terminator) it won't be processed, so it will wait until the next part is received and then it will be processed. (lets say that my sequence of data units, is difference messages (each having a message terminator) which will obviously be processed one by one.

    Remember that an InputStream acquired from a different source (such as an HttpConnection or FileConnection) might not behave the same way even on the same device. So just because available() works the way you expect on a SocketConnection, does not mean it will in different situations.
    So, at least, for the Nokia 6120 classic, InputStream acquired from SocketConnection should act the same way, but still it is not required to do so ?

    1. How many devices do you plan to support in the future? There are several thousand different models currently in use. Do you plan to test on each one?
    The devices being supported aren't a problem at all, because i'll be testing every device that the system will be running on, so the devices which should be supported in the future are 100% controlled.

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Connection set blocking false...

    Quote Originally Posted by kurteknikk View Post
    How can i read until it blocks, without knowing the length of the data to be received?
    By reading. It will block when it blocks, until there is more data.

    Quote Originally Posted by kurteknikk View Post
    It is a sequence of discrete data units, and if a "field" hasn't been received all (i.e there is no message terminator) it won't be processed, so it will wait until the next part is received and then it will be processed. (lets say that my sequence of data units, is difference messages (each having a message terminator) which will obviously be processed one by one.
    What is the terminator? A specific character?

    Quote Originally Posted by kurteknikk View Post
    So, at least, for the Nokia 6120 classic, InputStream acquired from SocketConnection should act the same way, but still it is not required to do so ?
    If acquired by the same means, it's always the same class, so will always behave the same way. Hmmmm... saying that... for an HttpConnection, it might depend on the underlying protocol, so you might get different classes of object in that case, depending on device or network configuration. I'm pretty sure it won't be different classes for SocketConnections, though.

  7. #7
    Regular Contributor
    Join Date
    May 2009
    Posts
    95

    Re: Connection set blocking false...

    By reading. It will block when it blocks, until there is more data.
    Yes but i don't want it to block, i just want to read all the data without blocking... And that can be achieved only using the available, so if the available doesn't work well in some certain phone model i've still got a problem...

    What is the terminator? A specific character?
    Yes a specific character its a "~"

    I'm pretty sure it won't be different classes for SocketConnections, though.
    I will be using only a socketConnection so i hope that i won't have any problems if i test the available on each phone...

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Connection set blocking false...

    Quote Originally Posted by kurteknikk View Post
    Yes but i don't want it to block, i just want to read all the data without blocking... And that can be achieved only using the available, so if the available doesn't work well in some certain phone model i've still got a problem...
    You could have two threads. One sits reading from the socket (and blocking when there's no data). That way, you don't need to keep looping and polling to see if there's data. When data arrives, it get's queued to the second thread, which wakes up and processes it. This second thread looks something like:

    Code:
    public class Processor implements Runnable {
        private boolean exit;
        private Vector queue = new Vector();
    
        public synchronized void add(Message m) {
            queue.addElement(m);
            notifyAll();
        }
    
        public synchronized void kill() {
            exit = true;
            queue.removeAllElements();
            notifyAll();
        }
    
        public synchronized void run() {
            while (!exit) {
                while (!queue.isEmpty()) {
                    Message m = (Message) queue.firstElement();
                    queue.removeElementAt(0);
                    // process message here
                }
                // go to sleep until there's a message to process
                try {
                    wait();
                } catch (InterruptedException ie) {
                    // ignore
                }
            }
        }
    }

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    Quote Originally Posted by kurteknikk View Post
    I've got a problem that inputstream.read(byte[]) is blocking until data is received.
    Please, let us continue in your original thread. The problem is, you and your mate have (human) communication problems. In a professional MIDlet you do not use Input.available(). You use Threads and Input.read(). Period.

Similar Threads

  1. can one set default connection?
    By nokia_lin in forum Symbian Networking & Messaging (Closed)
    Replies: 4
    Last Post: 2009-07-21, 09:35
  2. connection to serial port
    By marluzch in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2006-11-20, 19:47
  3. Can I set permissions to always allow a network connection?
    By dlane99 in forum Mobile Java General
    Replies: 3
    Last Post: 2005-03-30, 13:22
  4. Set automatically pin code for an incoming connection in Bluetooth.
    By ajsanchez in forum Symbian Networking & Messaging (Closed)
    Replies: 14
    Last Post: 2004-10-27, 15:09
  5. How is GPRS connection security set up?
    By Nokia_FAQ in forum Multimodecards
    Replies: 1
    Last Post: 2002-05-14, 16:43

Posting Permissions

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