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
New Developer
nroy
Posts: 14
Registered: ‎08-18-2008
My Device: Not Specified

Datagram Connection sending/receiving

[ Edited ]

From the JavaDocs:

 

A datagram connection can be opened in a "client" mode or "server" mode. If the "//{host}" part is missing then the connection is opened as a "server" (by "server", we mean that a client application initiates communication). When the "//{host}" part is specified, the connection is opened as a "client".

Examples:

A datagram connection for accepting datagrams
datagram://:1234

A datagram connection for sending to a server:
datagram://123.456.789.12:1234

Note that the port number in "server mode" (unspecified host name) is that of the receiving port. The port number in "client mode" (host name specified) is that of the target port. The reply-to port in both cases is never unspecified. In "server mode", the same port number is used for both receiving and sending. In "client mode", the reply-to port is always dynamically allocated.

 

 

Is it then possible to create a DatagramConnection for both sending and receiving?

 

I have tried to create a server mode datagram, but fails with illegalargumentexception on

datagramConnection.newDatagram(datagramConnection.getMaximumLength(), "datagram://192.168.1.101:33383");

 

I have also tried to create a client connection with different send/receive ports:

 

DatagramConnection datagramConnection=(DatagramConnection)Connector.open("datagram://192.168.21.42:33383;7001);

 

 

This seems to work perfect for sending datagrams, but a call to receive() blocks indefinatly when it should have data (from a non-ui thread).

 

The closest i have come to getting both send/receive working is instanciating two DatagramConnections. One "Server" connection on the receive port and another "Client" connection with the full host string. 

DatagramConnection receiveDatagramConnection=(DatagramConnection)Connector.open("datagram://:7001")

DatagramConnection sendDatagramConnection= DatagramConnection)Connector.open("datagram://192.168.1.101:33383;7001");

 

 

This works beautifully in the simulator for both sending and receiving, but on an actual phone (replacing local IPs for actual ones), throws an exception that the port is already bound, even after using differet port numbers.  Testing is being done using a CDMA phone, so APN information is irrelevant at this time. 

 

At this point, the only solution I see is to use a separate "server" mode datagram connection that does nothing but receive on a port different from the "client" mode reply port. Any additional information and help would be greatly appreciated. 

 

 

Nathan Roy | Metova

www.metova.com

Message Edited by nroy on 08-25-2008 02:47 PM
Message Edited by nroy on 08-25-2008 02:54 PM
Please use plain text.
Administrator
MSohm
Posts: 14,268
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Datagram Connection sending/receiving

Your suggestion solution is correct.  If you wish to open 2 different connections (one to listen and one to make an outbound connection) you will need to use 2 different ports.

 

You can read/write on the same connection and send/receive data using it.

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
Please use plain text.
New Developer
nroy
Posts: 14
Registered: ‎08-18-2008
My Device: Not Specified

Re: Datagram Connection sending/receiving

Mark, thank you for your reply. I would prefer to use the same connection for both sending/receiving and your reply seems to indicate this is possible, but as in my post, receive() never returns when there should be a reply. I have created a test case below. The message gets to the server, but the reply is not picked up by the blackberry application:

 

 

setDatagramConnection((DatagramConnection)Connector.open("datagram://192.168.21.32:33343;7001"); Message testMessage = new Message(); testMessage.setBody( "HELLO" ); testMessage.setControlCode( Message.CONTROL_CODE_POST ); testMessage.setReturnPort( preferences.getReceivePort() ); testMessage.setSender( "METOVA" ); testMessage.setSessionId( Settings.TEST_SESSIONID_JENNY ); byte[] bytes = testMessage.toString().getBytes(); Datagram datagram = getDatagramConnection().newDatagram( getDatagramConnection().getMaximumLength() ); datagram.setData( bytes, 0, bytes.length ); getDatagramConnection().send( datagram ); getDatagramConnection().receive( datagram ); String reply = new String( datagram.getData(), datagram.getOffset(), datagram.getLength() ); getLog().debug( "Received a Datagram:\n" + reply );

 

 

 

 

 

Please use plain text.
Administrator
MSohm
Posts: 14,268
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Datagram Connection sending/receiving

If you do listen on a different port on the BlackBerry, does the message arrive?

 

What BlackBerry model and handheld software version are you testing on?

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
Please use plain text.
New Developer
nroy
Posts: 14
Registered: ‎08-18-2008
My Device: Not Specified

Re: Datagram Connection sending/receiving

[ Edited ]

Mark,

 

If I include return path information in each outgoing message and have the server disregard the reply port at the packet level, two way communication only works on Sprint (Tested with BB 8330 with OS 4.3.127). Given the following scenario where i have two ports for each sending and receiving: 

 

 

DatagramConnection receiveDatagramConnection=(DatagramConnection)Connector.open("datagram://:7002")

DatagramConnection sendDatagramConnection= DatagramConnection)Connector.open("datagram://192.168.1.101:33383;7001");

 

I send a datagram with:

 

Datagram datagram = connection.newDatagram(connection.getMaximumLength());
datagram.setData( bytes, 0, bytes.length );
connection.send( datagram );

 

I am only able to send/receive with Sprint. I do not know for sure, but I suspect that the gateway somehow uses the reply port at the packet level to open a route back to the phone for the return communication. AT&T Orange (Cingular) GSM phones are only able to send, but not receive.

 

Regards, 

Nathan

 

 

Message Edited by nroy on 09-11-2008 11:50 AM
Please use plain text.
Administrator
MSohm
Posts: 14,268
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Datagram Connection sending/receiving

192.168.1.101 is a non routable IP address.  Where are you trying to connect to?  You'll need to use your destination server's public IP address.
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
Please use plain text.
New Developer
nroy
Posts: 14
Registered: ‎08-18-2008
My Device: Not Specified

Re: Datagram Connection sending/receiving

This is for the simulator and local testing. The real IP is used on the phone.
Please use plain text.
Administrator
MSohm
Posts: 14,268
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Datagram Connection sending/receiving

Since it is working on one carrier, but not another I recommend contacting the carrier to verify that they support this on their network.
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
Please use plain text.