views:

1101

answers:

1

Hi,

I am trying to send data using UDP (datagram). I am not able to test application on simulator. I tried running MDS first and then simulator,but it did not work. The error is displayed as Port 8080 already in use on BlackBerry simulator console. How do I change port in simulator? The UDP port to which I am connecting is localhost:5014 I am using simulator for BlackBerry Pearl 8100. I am new to BlackBerry networking. Please help me.

Thanks.

+3  A: 

On the Blackberry forum there are comments about issues with datagram under 4.5.0.x up to 4.5.0.83. No wonder there are no UDP samples in sdk. You always can download 8100 with 4.5.0.108 simulator from http://na.blackberry.com/eng/developers/

Another thing is to use ip, although hostname is allowed in api reference, but when you use MDS simulator it grabs localhost alias.

In the following code you have simple server which is listening to port 135, and bb client which is sending data packet to 127.0.0.1 on port 135.

Desktop server code:

public static void main(String[] args) {
 byte[] inBuff = new byte[32];
 DatagramSocket socket;
 try {
  socket = new DatagramSocket(137);
  DatagramPacket pckt = new DatagramPacket(inBuff, inBuff.length);
  while (true) {
   socket.receive(pckt);
   System.out.println(new Date() + " " + pckt.getAddress()
     + ":" + pckt.getPort());
   socket.send(pckt);
  }
 } catch (Exception e) {
  System.out.println(e.getMessage()+":");
  System.out.println(e.getClass().getName());
 }
}

BlackBerry client code (tested with Bold 8900 under 4.6.1):

 UDPDatagramConnection connection = null;
 byte[] outBuff = "Hello!".getBytes();
 Datagram outDatagram = null;
 try {
  connection = (UDPDatagramConnection) Connector
    .open("datagram://127.0.0.1:137");
  outDatagram = connection.newDatagram(outBuff, outBuff.length);
  connection.send(outDatagram);
  System.out.println("Datagram packet was sent");
 } catch (Exception e) {
  System.out.println(e.getMessage()+":");
  System.out.println(e.getClass().getName());
 }
Max Gontar
I am new to this BB development. How do I access the UDPDatagramConnection? Is this a library I have to import? I am using the Visual Studio plugin from RIM for my BB development.
Saif Khan
BB VS plugin is used for RAD apps development and its not j2me, so there is no access to UDPDatagramConnection. To run this code you can use Eclipse BB plugin or BB JDE. To ask about same functionality on VS plugin I would suggest to create separate question.
Max Gontar