01-07-2013 01:55 PM
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
https://hotfile.com/dl/187500204/ab4abc1/DEMOJAVAC
01-07-2013 11:12 PM
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:/
//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);
}
}
}
01-08-2013 08:21 AM
Any idea how to operate the UDP server with the data plan, This problem give me gray hair?
01-08-2013 09:57 AM
Why do you think it is possible to do this on the data plan?
01-08-2013 10:04 AM
Because with wifi works, then I guess with data plan should work.
01-08-2013 12:22 PM
Reading seems the APN may affect, but I still don't understand
01-08-2013 01:18 PM
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-Develo
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?
01-08-2013 02:04 PM
Thank you very much for the reply. I'll check what alternatives I have for my problem.
01-10-2013 11:18 AM
Talking to the operator they told me I should create a private APN to create the server on BB.
01-10-2013 11:37 AM
Never heard of that. Let us know how you get on.