Troubleshooting guide

126
BlackBerry Java Development Environment Development Guide
Applications that use socket connections typically require significantly more bandwidth than applications
that use HTTP connections.
Datagram connections
Datagrams are independent packets of data that applications send over networks. A Datagram object is a
wrapper for the array of bytes that is the payload of the datagram.
Use a datagram connection to send and
receive datagrams.
Use datagram connections
To use a datagram connection, you must have your own infrastructure to connect to the wireless network,
including an APN for GPRS networks.
Using UDP connections requires that you work closely with service
providers. Verify that your service provider supports UDP connections.
Task Steps
Before opening a socket connection,
verify that the BlackBerry device is in a
wireless coverage area.
>Use the CoverageInfo class and CoverageStatusListener interface of the
net.rim.device.api.system package to make sure that the BlackBerry device is in a
wireless coverage area.
Open a Socket connection. >Invoke Connector.open(), specifying socket as the protocol. Applications must input their
local machine IP explicitly because localhost is not supported.
private static String URL = "socket://<local machine IP>:4444";
StreamConnection conn = null;
conn = (StreamConnection)Connector.open(URL);
Send and receive data. >Invoke openInputStream() and openOutputStream().
OutputStreamWriter _out = new
OutputStreamWriter(conn.openOutputStream());
String data = "This is a test";
int length = data.length();
_out.write(data, 0, length);
InputStreamReader _in = new
InputStreamReader(conn.openInputStream());
char[] input = new char[length];
for ( int i = 0; i < length; ++i ) {
input[i] = (char)_in.read();
};
Close the socket connection. >Invoke close() on the input and output streams and the socket connection.
_in.close();
_out.close();
conn.close();
Each of the close() methods throws an IOException. Make sure that the application
implements exception handling.