![]() |
||||||||
|
|
||||||||
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 { public: 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:
ZCom_Address addr; addr.setPort(broadcast_port); 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 } };
1.4.6-NO