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: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

UDPDatagramConnection.receive timeout

My application is currently set up such that it sends a datagram to the server and waits for a response. The Datagram communication and the program proper are on separate event Threads, but the main BG Thread still waits for the UDP Thread to complete its operations, as if waiting for a signal takes 100% of the available CPU Threads. First off, how do I fix this so they can run simulataneously? My Background thread sleeps on a timer, and I don't want the timer interrupted. Furthermore, I want the listening operation to timeout after some abrbitrary length of time -- say 30 seconds. It will listening 3 times, and after the third timeout will assume that the server didn't receive the packet and store it for future sending. UDPDatagramConnection.receive doesn't have any overloaded methods to specify timeout, so how do I go about accomplishing this?

 

Thanks,

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: UDPDatagramConnection.receive timeout

UPDATE: Basically what I wanted to do was impossible. One of the other developers at my company was able to work around the issue by creating a separate Thread that constantly listens for inbound datagrams without disrupting the current event Thread.

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: UDPDatagramConnection.receive timeout

Basically, you don't want the main thread to wait for the data to come in.  What I could do is to have the backgroud thread, in your case, the thread that sends the datagram, to wait (with a timeout) after sending the 'request'. I would then have another thread to receive the incoming datagram and upon receiving  a datagram, it notifys the waiting thread.

Developer
Posts: 168
Registered: ‎11-22-2009
My Device: 9000 Bold, 8300 Curve

Re: UDPDatagramConnection.receive timeout

I've been having abit of trouble with this myself. I set up two threads, one as a server to receive and one as a client to send. What I'm not sure of is how to tell when I've received a packet. Following some examples I set up a connection to receive in my server thread and then run a loop like this:

 

 

  while(true)
        {

            Datagram receive_packet = server_socket.newDatagram(receive_data,receive_data.length); 
            server_socket.receive(receive_packet);
            
            byte[] bytes = receive_packet.getData();
            String address = receive_packet.getAddress();
            String msg = new String(bytes);
            
            System.out.println( "Received packet from " + address + " : " + msg);
 
          
               // break;
          
         }

 

Does this thread just wait at the server_socket.receive part?

 

 

Also, I am receiving with just this url specified "udp://" is that correct? For wifi I am also testing "udp://;interface=wifi"

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: UDPDatagramConnection.receive timeout

I should explain the construction of my program a little better:

 

The main event Thread really doesn't do anything; it starts the background Thread and then fizzles into nothingness. The BG Thread sleeps on a timer, waking up periodically to sending a message. When it sends the message, it starts a UDP out Thread and goes back to sleep (I fixed that issue, by the way. I was making a static method call rather than re-intializing the Thread). The UDP Thread accesses a static GPS class to get some information and then sends it back to the UDP Thread so it can build and send the packet. As of yesterday a separate Thread listens for server acknowledgement parallel to the BG Thread. So at any given time I'll have 2 to 3 Threads going, which shouldn't be too big a deal. The only way I could fix my biggest issue above, however, is by completely forgetting about the timeout -- My listening Thread doesn't close. When it receives and acknowlegement it begins processing the information, and then goes right back to listening until the program ends.

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: UDPDatagramConnection.receive timeout

The listening thread should always be listening/receiving. When a package arrives, it should post some kind of information to a common variable such that the sending thread can also access. Hence, the sending thread can send a request to the server and then waits for say 30 seconds. When it wakes up, it checks that common variable for some kind of indication. If there weren't any indication, waits for another 30 seconds.

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

Re: UDPDatagramConnection.receive timeout

 


schepurny wrote:

I've been having abit of trouble with this myself. I set up two threads, one as a server to receive and one as a client to send. What I'm not sure of is how to tell when I've received a packet. Following some examples I set up a connection to receive in my server thread and then run a loop like this:

 

 

  while(true)
        {

            Datagram receive_packet = server_socket.newDatagram(receive_data,receive_data.length); 
            server_socket.receive(receive_packet);
            
            byte[] bytes = receive_packet.getData();
            String address = receive_packet.getAddress();
            String msg = new String(bytes);
            
            System.out.println( "Received packet from " + address + " : " + msg);
 
          
               // break;
          
         }

 

Does this thread just wait at the server_socket.receive part?

 

 

Also, I am receiving with just this url specified "udp://" is that correct? For wifi I am also testing "udp://;interface=wifi"


 

Are you  tring to communicate from one part of the program to another part of the program via UDP?  I am totally lost. A server UDP socket URL is something like "udp://:123;interface=wifi" 123 is the UDP port. Also, I don't thing the BB supports the loop back interface.

 

Developer
Posts: 168
Registered: ‎11-22-2009
My Device: 9000 Bold, 8300 Curve

Re: UDPDatagramConnection.receive timeout

I am very lost myself. This is definitely not something that I am strong on.

 

What I am trying to do is send a packet to a public STUN server while I have a separate thread with a udp port open for it to send me back a packet. I think I just am not getting what port I should be listening on.

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: UDPDatagramConnection.receive timeout

I'm aware that Blackberries do not support loopback operations -- at least not in any practical form. Eventually this will go to the company servers, but it seems rather pointless to send hundreds of test packets to the main servers when I have a perfectly good machine right here.

 

That said, thanks for the help guys.

 

P.S. - Re-reading my original post, I apologize for "it will listening" XD

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: UDPDatagramConnection.receive timeout

 


schepurny wrote:

I am very lost myself. This is definitely not something that I am strong on.

 

What I am trying to do is send a packet to a public STUN server while I have a separate thread with a udp port open for it to send me back a packet. I think I just am not getting what port I should be listening on.


 

 

This is actually very easy. I access public STUN servers from by BB apps all time.

 

This is what I do.

 

Create a udp socket udpA

Common variable stunRet

 

thread 1

      send STUN request

      wait for 30 second then check stunRet

      if timeout, go back send STUN request

     else we got our external IP address and Port

end thread 1

 

thread 2

    while (true)

          receive udp

          if it is return from stun server, set the info in stunRet and notify thread 1

    end while

end thread 2

 

Note: In order the maintain the NAT entry in the router,  you have maintain traffic on the udp socket at least 1 packet every now and then.