Java Reference
In-Depth Information
byte[] payload = message.toString();
try {
UDPDatagramConnection conn = null;
conn = (UDPDatagramConnection)
Connector.open("datagram://localhost:1234");
Datagram datagram = conn.newDatagram(payload, payload.length);
conn.send(datagram);
} catch(IOException ioe) { ... }
Server mode connections are for receiving (and replying to) incoming
datagrams. To open a datagram connection in server mode, we use a URI
of the following form:
datagram://:1234
The port number in this case refers to the port on which the local
device is listening for incoming datagrams. Sample code for receiving
incoming datagrams is given below:
try {
UDPDatagramConnection dconn = null;
dconn = (UDPDatagramConnection)Connector.open("datagram://:1234");
Datagram dg = dconn.newDatagram(300);
while(true) {
dconn.receive(dg);
byte[] data = dg.getData();
...
}
} catch(IOException ioe) { ... }
A signed MIDlet suite which contains MIDlets that open datagram
connections must explicitly request permission to open client connections
or server connections:
MIDlet-Permissions: javax.microedition.io.Connector.datagram,
javax.microedition.io.Connector.datagramreceiver, ...
If the protection domain to which the signed MIDlet suite would
be bound grants, or potentially grants, the requested permissions, the
MIDlet suite can be installed and the MIDlets it contains will be able to
open datagram connections. This can be done automatically or with user
permission, depending on the security policy in effect. Whether untrusted
MIDlets can open datagram connections depends on permissions granted
to MIDlet suites bound to the untrusted protection domain.
Search WWH ::




Custom Search