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
Highlighted
Contributor
Posts: 13
Registered: ‎01-07-2013
My Device: 8310
My Carrier: Claro

Blackberry server UDP

Regards, 

 

I made a very simple application for Blackberry which behaves as a UDP server, the problem is that even in the emulator works fine on the computer doesn't, at least not with the edge connection with the wifi connection work well, here I attached the projects if some kind soul  can lend me a hand. Thanks in advance.

 

https://hotfile.com/dl/187499947/7fac1cb/ServerUDP.rar.html
https://hotfile.com/dl/187500204/ab4abc1/DEMOJAVACliente.rar.html

Contributor
Posts: 13
Registered: ‎01-07-2013
My Device: 8310
My Carrier: Claro

Re: Blackberry server UDP

The code that I use is:

 

import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.microedition.io.UDPDatagramConnection;

import net.rim.device.api.io.DatagramConnectionBase;
import net.rim.device.api.io.DatagramStatusListener;
import net.rim.device.api.io.DatagramStatusListenerUtil;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
private LabelField _statusField;
private StringBuffer _status;

/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
setTitle("MyTitle");

_status = new StringBuffer();


_statusField = new LabelField();
_statusField.setText("Esperando...");
add(_statusField);

//servidor.
UDPThread server = new UDPThread(this);
server.start();
}

/**
* Updates the status field
* @param text The text with which to update the status field
*/
void updateStatus(String text)
{
_status.append(text + '\n');
_statusField.setText(_status.toString());
}

void mostrarMensaje(final String mensaje){
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
//Dialog.alert("\nError procesando exito de comunicación. "
// + e.getMessage());
updateStatus(mensaje);
}
});
}

/**
* Hilo servidor UDP
* @author linux
*
*/
class UDPThread extends Thread implements DatagramStatusListener
{
public boolean loop;
DatagramConnectionBase dc;

private MyScreen screen;

UDPThread(MyScreen scr)
{
screen = scr;
}

public void run()
{
loop = true;

try
{
//si quiero wifi agrego ;interface=wifi
dc = (DatagramConnectionBase)Connector.open("datagram://:7/;deviceside=true", Connector.READ_WRITE, false);
//String myhost = System.getProperty("microedition.hostname");
dc.setDatagramStatusListener(this);
//_conn.setTimeout(500000); // 30 seconds

mostrarMensaje("mihost: "+dc.getLocalAddress());
mostrarMensaje("miport: "+dc.getLocalPort());

while (loop)
{
Datagram d = dc.newDatagram(4096);
dc.receive(d);
String payload = new String(d.getData());
//System.out.println("Datagram received: " + payload);
mostrarMensaje(payload);
}
}
catch (Exception e)
{
mostrarMensaje("Error: "+e.getMessage()+" : "+e.toString());
//System.out.println(e.toString());
}
}

public void sendDatagram(String host, String payload)
{
try
{
Datagram d = dc.newDatagram(payload.getBytes(), payload.length(), "datagram://"+host+":7");
dc.send(d);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}

public void updateDatagramStatus(int dgId, int code, Object context) {
// TODO Auto-generated method stub
String msg = " Datagram: " + dgId + " status: " + DatagramStatusListenerUtil.getStatusMessage(code);
mostrarMensaje("updateDgSt:" + msg);
}

}
}

 

 

Contributor
Posts: 13
Registered: ‎01-07-2013
My Device: 8310
My Carrier: Claro

Re: Blackberry server UDP

Any idea how to operate the UDP server with the data plan, This problem give me gray hair?

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

Re: Blackberry server UDP

Why do you think it is possible to do this on the data plan?

Contributor
Posts: 13
Registered: ‎01-07-2013
My Device: 8310
My Carrier: Claro

Re: Blackberry server UDP

Because with wifi works, then I guess with data plan should work. 

Contributor
Posts: 13
Registered: ‎01-07-2013
My Device: 8310
My Carrier: Claro

Re: Blackberry server UDP

Reading seems the APN may affect, but I still don't understand

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

Re: Blackberry server UDP

This is only my opinion, but I have think you have actually hit the nail on the head when you save with WiFi this works.  With wireless connection is won't. 

 

The problem is that the device, on the data network, does not really have an IP address.  And it certainly doesn't listen for random bits of data.  If these were sent over the wireless network the carrier networks would rapidly fill up data like this. 

 

Have a look at the following video which I think might explain it further:

http://supportforums.blackberry.com/t5/Java-Development/Networking-Transports-II/ta-p/446742

At various points, it will present a network diagram that shows how the device connects to the internet.  You will notice is all cases except WiFi, it connects through a gateway - it is this gateway that is available on the internet, NOT the device. 

For example, 3.0 minutes in you will see WAP - look for the carrier provided gateway.  At 7.0 minutes in you will see the BIS connection - here the network visible IP address is actually provided by the RIM infrastructure.

 

In short, the only way you can use your BB as a UDP server is to have the device on WiFi.

 

OK?

 

Contributor
Posts: 13
Registered: ‎01-07-2013
My Device: 8310
My Carrier: Claro

Re: Blackberry server UDP

Thank you very much for the reply. I'll check what alternatives I have for my problem.

Contributor
Posts: 13
Registered: ‎01-07-2013
My Device: 8310
My Carrier: Claro

Re: Blackberry server UDP

Talking to the operator they told me I should create a private APN to create the server on BB.

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

Re: Blackberry server UDP

Never heard of that.  Let us know how you get on.