•
To open a socket connection using BlackBerry MDS Services , append
deviceside=false
to the end of the
URL. BlackBerry device applications must input their local machine IP explicitly because
localhost
is not
supported.
private static String URL = "socket://local_machine_IP:
4444;deviceside=false";
StreamConnection conn = null;
conn = (StreamConnection)Connector.open(URL);
•
To open a socket connection over direct TCP, append the
deviceside=true
parameter to the end of the URL.
private static String URL = "socket://local_machine_IP:
4444;deviceside=true";
StreamConnection conn = null;
conn = (StreamConnection)Connector.open(URL);
•
To open a socket connection over direct TCP, specifying APN information, append the deviceside=true parameter to
the end of the URL and specify the APN over which the connection will be made. Specify the user name to connect
to the APN, and the password if required by the APN.
private static String URL = "socket:
//local_machine_IP:
4444;deviceside=true;apn=internet.com;tunnelauthusername
=user165;tunnelauthpassword=user165password";
StreamConnection conn = null;
conn = (StreamConnection)Connector.open(URL);
5.
Use
openInputStream()
and
openOutputStream()
to send and receive data.
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();
};
6.
Invoke
close()
on the input and output streams and the socket connection. Each of the
close()
methods throws an
IOException
. Make sure that the BlackBerry device application implements exception handling.
_in.close();
_out.close();
conn.close();
Use a datagram connection
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.
Development Guide
Connections
58