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
Developer
rgelb1
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

The message to nowhere.

I am a bit confused by how SocketConnection actually works.  I sent a message to a completely non-existent address and it did not error out.  Shouldn't TCP stack only send messages to places it can connect to?  For instance, the code below. I know that the IP address 45.66.36.22 does not exist anywhere?  And yet the code does not cause any exceptions. 

 

String name = "socket://45.66.36.22:9100;deviceside=false";

 

SocketConnection sc = (SocketConnection) Connector.open(name);                     
DataOutputStream dos = sc.openDataOutputStream();

 

byte[] sendData = sendString.getBytes();           
dos.write(sendData);   

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: The message to nowhere.

Don't think it is actually sent until you do a flush (dos.flush():smileywink:.  Once you do a flush, I think your connection will time out.  Give it a try.
Please use plain text.
Developer
rgelb1
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: The message to nowhere.

Nope, I've added a code to do .flush per your comment - still sends fine to non-existent addresses, besides, the documentation states that .close calls .flush for you.  In fact, I've compiled the program and you can download and try it here:  http://www.vbrad.com/download/SocketTester.jad

 

So, for clarification, I am trying to hit an IP on the internal enterprise network, e.g. my blackberry is tethered to the BlackBerry Enterprise Server, that's installed at my employer's data center.

 

Here is the appended code

 

    private boolean SendData(String IpAddressPort, String message)
    {
        String name = "socket://" + IpAddressPort;        
        SocketConnection sc = null;
        DataOutputStream dos = null;
       
        try
        {
            sc = (SocketConnection) Connector.open(name);             
       
            dos = sc.openDataOutputStream();
            byte[] sendData = message.getBytes();               
           
            dos.write(sendData);
            dos.flush();
           
            return true;           
        }
        catch (IOException ex)
        {
            Dialog.alert(ex.getMessage());
            return false;
        }
        finally
        {
            if (dos != null)
            {
                try { dos.close(); }
                catch (IOException ex) {Dialog.alert(ex.getMessage());}
            }
           
            if (sc != null)
            {
                try { sc.close(); }
                catch (IOException ex) {Dialog.alert(ex.getMessage());}
            }
        }
    }    

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,673
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: The message to nowhere.

Does the code actually complete when using the invalid IP address or does it block?  Note that the default time out on BlackBerry is 2 minutes.  So the BlackBerry handheld will wait for 2 minutes before throwing an exception stating that it can not connect.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
rgelb1
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: The message to nowhere.

Yes, it does complete, because I actually put up a Dialog.alert saying that it sent successfully, if it makes it through the method.  You can try it with the application I put up, or I can post the entire project. 
Please use plain text.
Developer
rgelb1
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: The message to nowhere.

One more note.  I am hitting an IP address internal to the enterprise, in other words it goes through the BlackBerry enterprise server (as you can see from the connection string).

 

If I add deviceside=true and try to hit a non-existing address on the internet, it does throw up an error.  But that's not what I need.

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,673
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: The message to nowhere.

I have confirmed that this is a limitation when making a connection to an IP address through the MDS Connection Service.  There are a couple of work arounds:

 

1.  Connect to a server name instead of an IP, which results in a DNS exception for non existent entries.

2.  Perform a read after your write.  This should block for data and eventually time out and thrown an exception.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
rgelb1
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: The message to nowhere.

Great, so I am not crazy.

 

1.  I am connecting to a device, which does not have a server name, just an IP address.

2.  How do I control the timeout on the read? I can't have it blocking for too long.

 

Thanks.

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,673
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: The message to nowhere.

Please see this link.

 

How To - Control the connection timeout for TCP connections through BlackBerry Mobile Data System Connection Service
Article Number: DB-00526

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/How_To_...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
rgelb1
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: The message to nowhere.

Ok, here is what I got.  This approach sort of works, but not to my satisfaction.  Here are the results.

 

I set the timeout to 3 seconds.  All the exceptions occur on the .read() method (this is important) .

 

Scenario 1. If I send a message to an IP Address that is listening on the specified port (but does not send anything in return), then I get the following IOException: Local connection timed out after ~3000

 

Scenario 2. If I send a message to an IP Address that exists, but is not listening on the specified port, then I get the following IOException: Connection refused: connect.

 

Scenario 3. If I send a message to an IP Address that does NOT exist, then I get the following IOException: Local connection timed out after ~3000.  Exactly the same as Scenario 1.

 

So, in effect, there is no way for me to tell whether message actually made it through or got sent to neverland. Am I missing something?

 

Thanks. 

 

 

 

Please use plain text.