Sun Microsystems V2.0 manual Client end, Server end

Page 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();

}

32

Image 32
Contents Page Page Contents Http protocol support Introduction Building and deploying Sun Spot applications Deploying and running a sample applicationBuild Successful Total time 3 seconds \MyApplication Ant -Dport=COM2 info Total time 4 seconds \MyApplication Total time 0 seconds \MyApplication Deploying a pre-existing jar Incorporating utility classes into your applicationOther user properties Manifest and resourcesExcluding files from the compilation Overview Using the BasestationBuilt-in properties Set up Introduction Base Station configurationRemote operation Connect a Sun Spot base station Using short names for SPOTs Managing keys and sharing Sun SPOTsTake suitable actions during over-the-air downloads BackgroundChanging the owner of a Sun Spot Sharing Sun SPOTsWhat is protected? Deploying and running a host applicationGenerating a new key-pair LimitationsConfiguring network features Your own host applicationIncorporating pre-existing jars into your host application Mesh routingLogging Hardware configurations and USB powerTrace route Page Thread priorities Overview of an applicationThreads Device Interface Sun Spot device librariesSun Spot device library Persistent properties Accessing flash memory Overriding the Ieee addressUsart Radio communication library Program Radiogram protocol Client end Server endYou can open server radiogram connections in a similar way Broadcasting Radio properties Turning the receiver off and on Shallow Sleep Conserving power using deep sleep modeMonitoring radio activity Activating deep sleep mode Deep SleepUSB inhibits deep sleep Preconditions for deep sleepingDeep sleep behaviour of the standard drivers Device Condition to permit deep sleepWriting a device driver Configuring the http protocol Http protocol supportConfiguring projects in an IDE Socket Proxy GUI modeDebugging Classpath configurationJavadoc/source configuration Ant selectapplication Configuring NetBeans as a debug client Configuring Eclipse as a debug clientUsing library suites Advanced topicsWith adderlib as your current directory, execute the command Use the command Using the spot client SpotSerialPortException other exception in serial port comms Property name Meaning ReferencePersistent system properties Contents of the arm directory Contents of the lib directoryMemory usage Start address Space UseContents of the bin directory preverify.exe Contents of the upgrade directory Contents of the tests directory