Introduction Manual Class Reference Header Reference

Sharing UDP Sockets

It is not only possible to run several servers in the same process, but also on the same UDP port. This is achieved by assigning IDs to ZCom_Control instances, which serve as identification when several controls use the same port.

Server Side

To start several ZCom_Controls on one port, you first need to assign the size of the control ID to ZCom_initSockets():
// this won't compile as ZCom_Control is an abstract class, apply the example to 
// a derived class
ZCom_Control *server1 = new ZCom_Control();
ZCom_Control *server2 = new ZCom_Control();

// use control ID size 1
server1->ZCom_initSockets(true, 9999, 0, 1);
server2->ZCom_initSockets(true, 9999, 0, 1);

The control ID size tells Zoidcom how many different control IDs should be possible. 1, as in this example, sizes it to 1 bit, allowing two different control IDs: 0 and 1. If you need more, the size must be increased. Note that Zoidcom needs to add the control ID to every packet sent by a ZCom_Control. Thus using a size of 32 means that all packets add 4 bytes of control id data.

Each control needs it's own unique ID from a possible range of 0 up to the above applied (size^2)-1:

server1->ZCom_setControlID(0);
server2->ZCom_setControlID(1);

Now two ZCom_Controls listen on the same UDP port, but each one will only receive packets directed to their control id.

Client Side

If a ZCom_Control server instance has been set up to use control IDs, all clients that want to connect to it, need to specify the ID's size as well the id itself, too.

ZCom_Control *client1 = new ZCom_Control();
ZCom_Control *client2 = new ZCom_Control();

// use control ID size 1
client1->ZCom_initSockets(true, 0, 0, 1);
client2->ZCom_initSockets(true, 0, 0, 1);

// connect client 1 to server 1
ZCom_Address target;
target.setAddress(eZCom_AddressUDP, 0, "localhost:9999");
client1->ZCom_Connect(target, NULL);

// connect client 2 to server 2
target.setAddress(eZCom_AddressUDP, 1, "localhost:9999");

ZCom_initSockets() needs the same control ID size as the servers, otherwise they won't understand the incoming packets at all. The second parameter to ZCom_Connect must be the target's control id, which in this example, are 0 and 1.


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