Sun Microsystems V2.0 Welding System User Manual


 
32
where
serverAddr
is the 64bit IEEE Address of the radio of the server, and
portNo
is a port number
in the range 0 to 255 that identifies this particular connection. Note that 0 is not a valid IEEE
address in this implementation. The port number must match the port number used by the server.
Data is sent between the client and server in datagrams, of type
Datagram
. To get an empty datagram
you must ask the connection for one:
Datagram dg = conn.newDatagram(conn.getMaximumLength());
Datagrams support stream-like operations, acting as both a
DataInputStream
and a
DataOutputStream
. The amount of data that may be written into a datagram is limited by its length.
When using stream operations to read data the datagram will throw an
EOFException
if an attempt is
made to read beyond the valid data.
A datagram is sent by asking the connection to send it:
conn.send(dg);
A datagram is received by asking the connection to fill in one supplied by the application:
conn.receive(dg);
Here's a complete example:
Client end
RadiogramConnection conn =
(RadiogramConnection)Connector.open("radiogram://0014.4F01.0000.0006:10");
Datagram dg = conn.newDatagram(conn.getMaximumLength());
try {
dg.writeUTF("Hello up there");
conn.send(dg);
conn.receive(dg);
System.out.println ("Received: " + dg.readUTF());
} catch (NoRouteException e) {
System.out.println ("No route to 0014.4F01.0000.0006");
} finally {
conn.close();
}
Server end
RadiogramConnection conn = (RadiogramConnection) Connector.open("radiogram://:10");
Datagram dg = conn.newDatagram(conn.getMaximumLength());
Datagram dgreply = conn.newDatagram(conn.getMaximumLength());
try {
conn.receive(dg);
String question = dg.readUTF();
dgreply.reset(); // reset stream pointer
dgreply.setAddress(dg); // copy reply address from input
if (question.equals("Hello up there")) {
dgreply.writeUTF("Hello down there");
} else {
dgreply.writeUTF("What???");
}
conn.send(dgreply);
} catch (NoRouteException e) {
System.out.println ("No route to " + dgreply.getAddress());
} finally {
conn.close();
}