Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 7
Registered: ‎11-19-2008
My Device: Not Specified

InputStream.read() block even if the network is disconnected

Hello,

 

I've encountered a strange behavior on a BB (8800 OS 4.5) when i tried to use socket connection:

1. When the socket is downloading a file from a server, I disconnected the network(Manage Connections - Turn All Connnections Off), and then I find the API InputStream.read(); is never returned. It means the API can not detect the connection is lost.

 

2. When the socket is downloading the file from the server, I kept the connections on, the file can be downloaded successfully.

 

I used the following suffixe:

1)  ;deviceside=true;ConnectionTimeout=600000;ConnectionSetup=delayed;retrynocontext=true; (for Direct TCP connection type)

2) Use edge network

 

 

codes segments:

public DataInputStream _is = null;

...

 _is = _conn.openDataInputStream();

...

cnt = _is.read(buffer,offset,readLen);       //here, if I disconnect the network, it blocks forever

 

 

Is there any way i can let the API to return at once in such situation?

 

 

 

Developer
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: InputStream.read() block even if the network is disconnected

How do you turn all the connectons off?

 

1. Do you turn your router off,

2. unplug the RJ-45 cable to the server,

3. turn the server off,

4. or turn off the radio?

 

Even with standard TCP, read  will block except 4. As for 1 to 3, read blocks until TCP times out which IIRC is 2 minutes.

 

New Developer
Posts: 7
Registered: ‎11-19-2008
My Device: Not Specified

Re: InputStream.read() block even if the network is disconnected

Thanks for your kind attention.

 

How to disconnect the net work:

On the main screen of BB 8800, click "Manage Connections" - "Turn All Connections Off".

 

I think what I do is same as your 4: turn off the radio on the device BB.

 

I have waited the read() for a whole day, and I wonder why the read() is still block, there is no time out??

 

 

 

Developer
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: InputStream.read() block even if the network is disconnected

[ Edited ]

You should try using the RadioStatusListener to see when you lose connection/coverage.

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
New Developer
Posts: 7
Registered: ‎11-19-2008
My Device: Not Specified

Re: InputStream.read() block even if the network is disconnected

YEATH, you are right.

 

My current solution is using RadioStatusListener to detect the radio status, if radioTurnedOff(), I interrupt the socket thread,

but I don't think it's a good solution.

 

What ever, read() should have the chance to return, even a exception, or have a timeout.

 

Developer
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: InputStream.read() block even if the network is disconnected

So it sits there and doesn't return an Exception? read() is supposed to throw an IOException. So I'm not sure why it isn't in your case.

 

Since it's supposed to be surrounded by a try...catch, see if you can print any information in the catch if you aren't already (more than just the stack trace).

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: InputStream.read() block even if the network is disconnected

Sounds like a bug in the OS. Try dropping most parameters from the connection URL, leaving only "deviceside=true". Also, try your app on some other device with a newer OS (v4.6+) to see whether this issue is specific to your device + OS combination.

 

P.S. Programmatically closing the connection once cellular coverage is lost isn't such a good idea: Carrier TCP connections (over the cellular interface) can survive temporary loss of coverage. Having said that, they cannot survive the switching off off the cellular network interface though.

New Developer
Posts: 7
Registered: ‎11-19-2008
My Device: Not Specified

Re: InputStream.read() block even if the network is disconnected

 

sample of my code segment:

 

                   try {

                         .....

                     System.err.println ...                                                        // can only see this log

                    cnt = _is.read(buffer,offset,readLen);                          // block here
                        if (cnt >= 0) {
                               System.err.println ...
                        } else {
                                System.err.println ...
                        }
                } catch (Exception ex) {
                        System.err.println ...

                }

....

 

 

if i turn off the radio, it blocks.

 

 

 

 

 

 

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: InputStream.read() block even if the network is disconnected

read() should block, this is reasonable. However, I don't think it should block forever, which is a bit concerning. I know on 4.6 that you should get an exception after a timeout period, but I'm not sure what the state of things is on 4.5.

New Developer
Posts: 7
Registered: ‎11-19-2008
My Device: Not Specified

Re: InputStream.read() block even if the network is disconnected

OK, thanks.

 

I should try it on 4.6.

After done it, I will post the result at here.