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 Contributor
Posts: 7
Registered: ‎03-27-2011
My Device: 9550
My Carrier: peoples

Problem of direct tcp connection, Choking Tunnel.open due to 'max tunnels' error; id=2

Hi Everyone

 

I am developing a application for blackberry 9550 storm(v5.0.0.1015). I got a problem on getting http conection via direct tcp on my blackberry device 9550 storm. My carriers is peoples.net, I have set it in the option.

 

I checked my application in the debug console. I found that the direct tcp connection work fine at the first time. But it fail at the second time. I check it in the debug console, it shown that "Choking Tunnel.open due to 'max tunnels' error; id=2" !!!!

 

I have already close the inputstream,outputstream,and conection for each connection accorading to the 

http://supportforums.blackberry.com/t5/Java-Development/Close-connections/ta-p/444878

 

Following is my coding:

 public static String HttpGet(String url) throws IOException {

 

    
 final String function = "Common::HttpGet()";
    
 HttpConnection httpConn = null;
 DataOutputStream outStream = null;
 InputStream inStream = null;
 String str="";

 if(ConnectionStatusThread.coverageWiFi){
 url=url+";interface=wifi";
 Common.Log(function,"","coverageWiFi");
 }else if(ConnectionStatusThread.coverageMDS){
 Common.Log(function,"","coverageMDS");
 }else if(ConnectionStatusThread.coverageBIS){
 Common.Log(function,"","coverageBIS");
 }else if(ConnectionStatusThread.coverageTCP){
 url=url+"?q=blackberry;deviceside=true";
 Common.Log(function,"","coverageTCP");
 }else{
 System.exit(0);
 }

 try {
 httpConn = (HttpConnection)Connector.open(url);
 httpConn.setRequestMethod(HttpConnection.GET);
 int rc = httpConn.getResponseCode();
 if (rc != HttpConnection.HTTP_OK) throw new IOException("HTTP response code: " + rc);
 inStream = httpConn.openDataInputStream();
            byte buffer[] = new byte[BUFFER_SIZE];
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int bytesRead=0;
         while(bytesRead!=-1) {
            bytesRead = inStream.read(buffer, 0, BUFFER_SIZE);
            if(bytesRead>0) byteArrayOutputStream.write(buffer, 0, bytesRead);
         }
         byteArrayOutputStream.flush();
         byte[] result = byteArrayOutputStream.toByteArray();
         byteArrayOutputStream.close();
        
         str = new String(result);

 }catch(Exception e) {
 e.printStackTrace();
 Log(function, "e", url);
 } finally {
 try{
 if (inStream != null)inStream.close();
 if (outStream != null)outStream.close();
 if (httpConn != null)httpConn.close();
 }catch (IOException e){
 System.out.println(e.getMessage());
 System.out.println("567");
 }
 }
     if(inStream!=null){
     inStream.close();
     }
    
     if(outStream!=null){
     outStream.close();
     }
 if(httpConn!=null){
     httpConn.close();
     }
 return str;
    }

 

 

Do anyone have got the solution on the issue of choking tunnel in direct tcp connection?

Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem of direct tcp connection, Choking Tunnel.open due to 'max tunnels' error; id=2

[ Edited ]

I have some issues with your code, which I will go into later.  Can I recommend that you download some known working code and try it yourself to see if fails like yours does.  This will help us decide where the problem is - your code or Verizon/OS bug.

 

So I would recommend that you try this:

http://supportforums.blackberry.com/t5/Java-Development/What-Is-Network-API-alternative-for-legacy-O...

and force it to use Direct TCP and see if it fails.:

 

Looking at your code, it seems to be that it does not follow the KB article when closing.  The KB article has this:

 

finally {
   if (in != null) {
       try {
in.close();
     } catch (IOException e) {
     }
  }
  if (out != null) {
      try {
          out.close();
} catch (IOException e) {
      }
  }
  if (conn != null) {
     try {
         conn.close();
     } catch (IOException e) {
     }
  }
}

Your code has this:

 

finally {

 try{
 if (inStream != null)inStream.close();
 if (outStream != null)outStream.close();
 if (httpConn != null)httpConn.close();
 }catch (IOException e){
 System.out.println(e.getMessage());
 System.out.println("567");
 }

 

These are not the same thing - what happens in your code if outStream throws an Exception?

 

And I suspect there is a chance of a problem here::

 if (outStream != null)outStream.close();

because this stream has already been flushed.  So this may throw an exception, meaning the httpConn close is missed.  And then later you have

 

    if(outStream!=null){

     outStream.close();
     }

 

This would throw an exception again, meaning that the httpConn is not closed, again.

 

I do have a number of other issues with your code:

 

1) url=url+"?q=blackberry;deviceside=true";

How come you only need this parameter when you are using Direct TCP?

 

2) else{

 System.exit(0);
 }

System.exit()?   Really!!!!!!

 

3) This code:

            byte buffer[] = new byte[BUFFER_SIZE];
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int bytesRead=0;
         while(bytesRead!=-1) {
            bytesRead = inStream.read(buffer, 0, BUFFER_SIZE);
            if(bytesRead>0) byteArrayOutputStream.write(buffer, 0, bytesRead);
         }
         byteArrayOutputStream.flush();
         byte[] result = byteArrayOutputStream.toByteArray();
         byteArrayOutputStream.close();
        
         str = new String(result);

 

This does not take into consideration how the data has been encoded.  Most Web Servers will UTF-8 encode data - the conversion you are doing above will mean that only characters recognized in ISO-8859-1 will be processed correctly.

 

It is also inefficient, in my opinion - how about using code like this:

 

byte [] responseBytes = IOUtilities.streamToBytes(in);

try {

    str = new String(responseBytes, "UTF-8");

catch (Exception e) {

    throw new RuntimeException("Unexpected decoding error: " + e.toString);

}

 

Of course is you are happy with ISO-8859-1, then you don't need the encoding or the try catch.

 

3) Your finally clause

finally {

 try{
 if (inStream != null)inStream.close();
 if (outStream != null)outStream.close();
 if (httpConn != null)httpConn.close();
 }catch (IOException e){
 System.out.println(e.getMessage());
 System.out.println("567");
 }
 }
 
Replace this code with code like that in the KB article.
 
4) This code
     if(inStream!=null){
     inStream.close();
     }
    
     if(outStream!=null){
     outStream.close();
     }
 if(httpConn!=null){
     httpConn.close();
     }
Is redundant and should be removed.
 
5) I must admit I don't like the way you throw IOException and also declare that this method  throws IOException.  I would recommend that, since this is your Exception, you should create your own.  In addition, since HttpConnection can itself throw IoException, there is possible confusion about what is actually going to handle the HttpConnection's IOException (and it will throw them from time to time), which having your own Exception would avoid. 
 
I hope this helps.
New Contributor
Posts: 7
Registered: ‎03-27-2011
My Device: 9550
My Carrier: peoples

Re: Problem of direct tcp connection, Choking Tunnel.open due to 'max tunnels' error; id=2

I have download the NetworkDiagnostic Project and run in my device. I tried to get http via direct tcp in that application, it can get http the first time, but when I tried to get http again without restart the application, "Choking Tunnel.open due to 'max tunnels' error; id=2 " occur!! ( I saw it in the debug console).

 

For using direct tcp, I only can get http smoothly the first time, and then choking for the second one.

I did not modify the code of NetworkDiagnostic project, I want to know whether this happan in other device?

Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem of direct tcp connection, Choking Tunnel.open due to 'max tunnels' error; id=2

OK, looks like you have a bug.  I presume that you have a proper Verizon phone with Verizon SIM and Verizon approved OS level.  if so, you can report a bug on Issue Tracker. 

 

But that is not going to help you out of your current pickle.  Really not sure what to suggest.  Can you connect with something other than Direct TCP?  Verizon support WAP for example. 

 

I'd still like you to fix up your connection code.....