Introduction Manual Class Reference Header Reference

LAN Broadcasting

LAN discovery allows a ZCom_Control to look for other ZCom_Controls through an UDP broadcast packet. To make it work, server and client need to implement a callback each.

The Server Part

The first thing the server has to do is tell the respective ZCom_Control that it should start listening for broadcasts. This is done by calling
class Server : public ZCom_Control
  // server class implementation
Server *server = new Server();
server->ZCom_setDiscoverListener(eZCom_DiscoverEnable, broadcast_port);

The control will open a socket on the given port or reuse the existing one in case the same socket has been used in ZCom_initSockets(), and now waits for broadcast packets to arrive. While most of the time, it is a good idea to keep the normal server port configurable, the broadcast port should be always the same for one application, otherwise a client can never know to which port a broadcast has to be sent. The server will queue the incoming broadcasts and process them as soon as ZCom_processInput() gets called. From there, the server callback ZCom_Control::ZCom_cbDiscoverRequest() gets called, which we implement now:

class Server : public ZCom_Control {
  bool ZCom_cbDiscoverRequest( const ZCom_Address &_addr, ZCom_BitStream &_request, ZCom_BitStream &_reply )
    // _addr is the address from which the broadcast came
    // _request is filled in by the client and can be read here
    // _reply can be filled in here and will get sent back to the client
    // return true to reply or false to not reply
    return true;

So, to sum up the server code:

The Client Part

The client has to execute something like this:
ZCom_Address addr;
addr.setControlID(control_id); // only if control ids are used
ZCom_BitStream *request = ZCom_Control::ZCom_createBitStream();
request->addString("me c/20 looking for a sweet s/20 to operate on, please reply");
control->ZCom_Discover(addr, request);

If there is a running ZCom_Control reachable by an UDP broadcast which has discover enabled, the client will receive a reply soon after executing the above code.

But it won't notice, because we didn't implement the client broadcast callback, yet:

class Client : public ZCom_Control
  void ZCom_cbDiscovered( const ZCom_Address & _addr, ZCom_BitStream &_reply )
    // _addr is the address from where the reply came
    // _reply is whatever the server filled into the _reply bitstream
To sum up the client:

It might happen that one call to ZCom_Discover() yields several results from the same host. Zoidcom will bind to all local network interfaces and broadcast from each of them, while the OS broadcasts to all interfaces itself in some or all cases. So when testing server and client on one single machine with two network interfaces, you will see 4 results.

This file is part of the documentation for Zoidcom. Documentation copyright © 2004-2008 by Jörg Rüppel. Generated on Sat Aug 16 15:26:51 2008 for Zoidcom by doxygen 1.4.6-NO