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

Port opening and closing.

Hello, this is the code i use for opening/closing a port.  For whatever reason, the device on the other end, won't respond to another request for about 45-60 seconds after the original request.  If I send the same information from a C# program, the device on the other end works fine.  What am I doing wrong?

 

         String sendString = "my data string";
        String IpAddress = "10.10.10.10";
        String name = "socket://" + IpAddress + ":" + "9100" + ";deviceside=false";
        
        
        try
        {
            SocketConnection sc = (SocketConnection) Connector.open(name);            
            sc.setSocketOption(SocketConnection.LINGER, 5);
        
            DataOutputStream dos = sc.openDataOutputStream();
            byte[] sendData = sendString.getBytes("ASCII");                
            
            dos.write(sendData);
            
            dos.flush();
            dos.close();            
            sc.close();
            
            // is this really needed?
            dos = null;
            sc = null;            
        }
        catch (Exception ex)
        {
            Dialog.alert(ex.getMessage());
        }

BlackBerry Development Advisor
Posts: 15,752
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Port opening and closing.

When you say the remote system won't respond to requests.  Does this mean that no client can connect to it or that the BlackBerry is unable to reconnect?  What is happening at the remote system at the time?  Has it received the connection close command?

 

This link explains how to close connections on a BlackBerry.

 

How To - Close connections 
Article Number: DB-00530

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

Re: Port opening and closing.

Mark,

 

Thanks for your response.  I did read the article a while back and wrote my code accordingly.  

I did some testing and basically speaking the target does not get the disconnect command immediately, despite me doing a .close().  The connection disconnects sometimes 30 seconds later, sometimes a minute later, sometimes longer.  I don't know whether this happens because of a timeout or the blackberry gets around to sending a disconnect command.

 

Is there something specifically wrong with my code?

 

I tried the same thing from C# code and the disconnect appeared immediately.  So not sure whether it's the my code, blackberry, java or the network.   Is there a better or alternative way to do a socket connection?

 

Thanks.

Developer
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: Port opening and closing.

Ok, I timed the disconnection.  Most of the time, it disconnects in 1 minute and 50 seconds.

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Port opening and closing.

You could probably run a packet capture on your remote system ( which is presumably local to you)

or just see what state you are in ( I think netstat will do this ).

 

http://support.microsoft.com/kb/137984 

 

Did you try to open the inputstream and just close it?

Developer
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: Port opening and closing.

Ok, I profiled the application on the server side using TcpSpy. 

 

Here are the results I got using the BlackBerry as the client.

socket (1088) accepted a connection on new socket (1104) [addr=10.10.167.158:1323]
socket (1104) retrieved one of its options [level=SOL_SOCKET, optname=SO_RCVBUF, optval=8192]
socket (1104) receiving data synchronously [len=8192]
socket (1088) accepting a connection synchronously
socket (1104) received 54 bytes of data

socket (1104) retrieved one of its options [level=SOL_SOCKET, optname=SO_RCVBUF, optval=8192]

socket (1104) receiving data synchronously [len=8192]

then, 1 minute and 50 seconds later

socket (1104) did not receive any data, closed gracefully by peer
socket (1104) closed; bytes received=54, bytes sent=0

 

 

Here is the result that I got using a c# app as the client

socket (1088) accepted a connection on new socket (1156) [addr=10.82.60.101:1118]

socket (1156) retrieved one of its options [level=SOL_SOCKET, optname=SO_RCVBUF, optval=8192]
socket (1156) receiving data synchronously [len=8192]
socket (1156) received 54 bytes of data

socket (1156) retrieved one of its options [level=SOL_SOCKET, optname=SO_RCVBUF, optval=8192]
socket (1156) receiving data synchronously [len=8192]
socket (1156) did not receive any data, closed gracefully by peer
socket (1156) closed; bytes received=54, bytes sent=0

 

So the difference here is that somehow the c# application closes the connection immediately (as evidenced by "closed gracefully by peer"), while the BlackBerry app closes the connection 1 minute and 50 seconds later.

 

Why is that?

Developer
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: Port opening and closing.

After more investigating, it appears that 1 minute and 50 seconds is a windows timeout thing.  Because I sent the message from the blackberry to my windows server, removed battery from the blackberry and the connection got closed on the windows side in 1:50 anyway.

 

So I am totally confused now.   It appears that the BlackBerry simply never sends a close command.  That's the only thing I can think of.

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Port opening and closing.

this suggests consuming data before closing the socket, I guess

you could try to open the irrelevant input side and then close,

 

http://java.sun.com/j2se/1.5.0/docs/guide/net/articles/connection_release.html

 

 

I haven't looked at TCP states in a while I thought netstat would work but I guess something like  nmap or

tcpdump may provide more info. I use showtraffic fairly often but again haven't tried to follow state of a tcp

connection lately.

 

 

 

Developer
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: Port opening and closing.

Ok, I rewrote the routine to include your suggestions, in addition to suggestions from the article (e.g. Linger set to 0).  Still, I encounter the same problem.  When you say that I should open an irrelevant input side and then close it, the way I have it in the code - is that how you envisioned it?  Or should I also attempt to read it, even through there is no data coming back?  Also .read is a blocking call, isn't it?

 

Is there anything else I can try?  Placing the thread to sleep perhaps?

 
        String sendString = "this is a test";

        String name = "socket://10.10.80.162:9100;deviceside=false";
       
        SocketConnection sc = null;
        DataOutputStream dos = null;
        DataInputStream dis = null;
       
        try
        {
            sc = (SocketConnection) Connector.open(name);             
            sc.setSocketOption(SocketConnection.LINGER, 0);
       
            dos = sc.openDataOutputStream();
            dis = sc.openDataInputStream();
           
            byte[] sendData = sendString.getBytes();           
           
            dos.write(sendData);       
           
            return true;           
        }
        catch (IOException ex)
        {
            Dialog.alert(ex.getMessage());
            return false;
        }
        finally
        {
            if (dis != null)
            {
                try { dis.close(); }
                catch (IOException ex) {Dialog.alert(ex.getMessage());}
            }           
           
            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());}
            }
        }

Developer
Posts: 108
Registered: ‎08-05-2008
My Device: Not Specified

Re: Port opening and closing.

The article  ( http://java.sun.com/j2se/1.5.0/docs/guide/net/articles/connection_release.html ) speaks of shutdownOutput to fix this issue, however, that does not seem to be available on the microedition.  Is there something else I could use?

 

Thanks.