Global Positioning System Reference
In-Depth Information
private void sendBytes(byte[] data, InetAddress group) {
DatagramPacket packet
= new DatagramPacket(data, data.length, group, port);
try { socket.send(packet); }
catch (java.io.IOException e) { ... }
}
}
In radio or television terminology, the SocketGate represents the broad-
casting station, the MulticastSocket the broadcasting technology, and the
InetAddress (es) the channel(s), which are all set in the constructor:
SocketGate () throws java.io.IOException
{
sender = new MulticastSocket ( port );
station = InetAddress.getByName( channel );
}
The server logic can broadcast an existing player record with
socketGate.sendPlayerStatusMessage( playerRecord )
The client, or rather receiver, has to implement one socket for each
channel
socket = new MulticastSocket( port ); // turn tuner on
socket.joinGroup( channel );
// dial a channel
// vice versa end with:
// socket.leaveGroup( channel );
// socket.close();
// turn tuner off
to be able to receive datagrams with
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, 256);
try {
socket.receive(packet);
// method blocks until a datagram is received
byte[] rcvd = packet.getData();
// create byte array
final PlayerRecord p
= new PlayerRecord(rcvd);// and player from byte array
anyMethod( player ); // propagate to local application
} catch (IOException e) { ... }
Note that first the datagrams data has to be retrieved, before the data is
actually turned back into useful information.
For completeness, it should be mentioned that the Bingo application
places socket.receive in a loop for repeated receptions, wraps the prop-
agation in a thread, and submits it to the Abstract Window Toolkit (AWT)
event-dispatching thread. The Bingo tutorial also describes the wins (shared
components update via broadcasting) and the optimistic handling of primi-
tive values. And, maybe the most interesting aspect: a broadcast datagram
 
Search WWH ::




Custom Search