This example shows you how to build a server that listens on multiple ports as both a TCP server and a UDP server. The basic structure is very similar to the Multi Echo Server
example. In fact it's a cross between that and the UDP Echo Server
example. You should go and read about both of those first and have a good understanding of how everything fits together. This document will only cover the differences between this example and the ones listed above.
This example is shipped with all licensed versions of The Server Framework and it requires the core server framework libraries (see here
for licensing options). You can always download the latest version of this example from here
; and although you will need the correct libraries to be able to build it you can look at the example code and see how it works and perhaps get ideas from it. A compiled, unicode release, build of this example is available on request if you require it for performance analysis of the framework.
As explained above, this is basically a cross between the other example servers listed above, however it does add a little twist of its own as it allows you to listen on different interfaces with different servers on the same port. This is useful for multi-homed servers which might present one server to the outside world on one network interface and another, slightly different, server to the internal network on the same port but on a different network interface.
But first, the comonality...
unsigned short port = commandLine.Port();
commandLine.NoWelcomeMessage() ? "" : "Welcome to echo server 1\r\n",
As you can see we have two different servers, a CTCPSocketServer and a CUDPSocketServer and we are adding instances of both to our server collection
so that we listen on the same port with both UDP and TCP servers.
Just for good measure we add several pairs of servers to different ports.
for (CAddressInfo::Iterator it = addressInfo.Begin(); it != addressInfo.End(); ++it)
const _tstring ipAddress = renderer.AsString(it);
const string message = commandLine.NoWelcomeMessage() ? "" : "Welcome to echo server 7 on: " + CStringConverter::TtoA(ipAddress) + "\r\n";
Finally we iterate through all of the addresses available in the machine and listen on the same port on each address with a different server. Note that on machines with IPv6 and IPv4 installed this will cause us to listen on all IPv6 adapters and all IPv4 adapters.