title: How can I run miltiple ServerApplications with POCO C++?
body: <p>I've started learning <a href="https://pocoproject.org/" rel="nofollow noreferrer">POCO C++ library</a> and I'm stuck while trying to run 2 servers in the same application (so that they can use some common runtime variables). These are 2 different servers, one of them is TCP TimeServer and the other one is simple UDP EchoServer. The code:</p>
  
  <pre><code>#include "Poco/Net/TCPServer.h"
  #include "Poco/Net/TCPServerConnection.h"
  #include "Poco/Net/TCPServerConnectionFactory.h"
  #include "Poco/Net/TCPServerParams.h"
  #include "Poco/Net/StreamSocket.h"
  #include "Poco/Net/ServerSocket.h"
  #include "Poco/Net/DatagramSocket.h"
  #include "Poco/Timestamp.h"
  #include "Poco/DateTimeFormatter.h"
  #include "Poco/DateTimeFormat.h"
  #include "Poco/Exception.h"
  #include "Poco/Util/ServerApplication.h"
  #include "Poco/Util/Option.h"
  #include "Poco/Util/OptionSet.h"
  #include "Poco/Util/HelpFormatter.h"
  #include &lt;iostream&gt;
  
  
  using Poco::Net::ServerSocket;
  using Poco::Net::StreamSocket;
  using Poco::Net::TCPServerConnection;
  using Poco::Net::TCPServerConnectionFactory;
  using Poco::Net::TCPServer;
  using Poco::Timestamp;
  using Poco::DateTimeFormatter;
  using Poco::DateTimeFormat;
  using Poco::Util::ServerApplication;
  using Poco::Util::Application;
  using Poco::Util::Option;
  using Poco::Util::OptionSet;
  using Poco::Util::HelpFormatter;
  
  
  class TimeServerConnection : public TCPServerConnection
  {
  public:
      TimeServerConnection(const StreamSocket&amp; s, const std::string&amp; format) :
          TCPServerConnection(s),
          _format(format)
      {
      }
  
      void run()
      {
          Application&amp; app = Application::instance();
  
          bool isOpen = true;
          Poco::Timespan timeOut(10, 0);
          unsigned char incommingBuffer[1000];
  
          app.logger().information("SYSLOG from " + this-&gt;socket().peerAddress().toString());
  
          while (isOpen) {
              if (socket().poll(timeOut, Poco::Net::Socket::SELECT_READ) == false) {
                  std::cout &lt;&lt; "TIMEOUT!" &lt;&lt; std::endl &lt;&lt; std::flush;
              } else {
                  int nBytes = -1;
                  try {
                      nBytes = socket().receiveBytes(incommingBuffer, sizeof(incommingBuffer));
                      std::cout &lt;&lt; incommingBuffer &lt;&lt; std::endl;
                  } catch (Poco::Exception&amp; exc) {
                      std::cerr &lt;&lt; "Network error: " &lt;&lt; exc.displayText() &lt;&lt; std::endl;
                      isOpen = false;
                  }
  
                  if (nBytes == 0) {
                      std::cout &lt;&lt; "Client closes connection!" &lt;&lt; std::endl &lt;&lt; std::flush;
                      isOpen = false;
                  } else {
                      std::cout &lt;&lt; "Receiving nBytes: " &lt;&lt; nBytes &lt;&lt; std::endl &lt;&lt; std::flush;
                  }
              }
          }
  
          try
          {
              Timestamp now;
              std::string dt(DateTimeFormatter::format(now, _format));
              dt.append("\r\n");
              socket().sendBytes(dt.data(), (int)dt.length());
          }
          catch (Poco::Exception&amp; exc)
          { app.logger().log(exc); }
      }
  
  private:
      std::string _format;
  };
  
  
  class TimeServerConnectionFactory : public TCPServerConnectionFactory
  {
  public:
      TimeServerConnectionFactory(const std::string&amp; format) :
          _format(format)
      {
      }
  
      TCPServerConnection* createConnection(const StreamSocket&amp; socket)
      { return new TimeServerConnection(socket, _format); }
  
  private:
      std::string _format;
  };
  
  
  class UDPServer : public Poco::Util::ServerApplication
  {
  public:
      UDPServer(){}
      ~UDPServer(){}
  
  protected:
      void initialize(Application&amp; self)
      {
          loadConfiguration(); // load default configuration files, if present
          ServerApplication::initialize(self);
      }
  
      void uninitialize() { ServerApplication::uninitialize(); }
  
      int main(const std::vector&lt;std::string&gt;&amp; args)
      {
  
          unsigned short port = (unsigned short)config().getInt("udpport", 9002);
          std::cout &lt;&lt; "[UDP] Using port " &lt;&lt; port &lt;&lt; std::endl;
          std::string format(config().getString("TimeServer.format", DateTimeFormat::ISO8601_FORMAT));
  
          Poco::Net::SocketAddress socketaddress(Poco::Net::IPAddress(), 9001);
          Poco::Net::DatagramSocket datagramsocket(socketaddress);
          char buffer[1024]; // 1K byte
  
          while (1) {
              Poco::Net::SocketAddress sender;
              int n = datagramsocket.receiveFrom(buffer, sizeof(buffer) - 1, sender);
              buffer[n] = '\0';
              std::cout &lt;&lt; sender.toString() &lt;&lt; ":" &lt;&lt; buffer &lt;&lt; std::endl;
          }
          return 0;
      }
  };
  
  class TimeServer : public Poco::Util::ServerApplication
  {
  public:
      TimeServer() : _helpRequested(false)
      {
      }
  
      ~TimeServer()
      {
      }
  
  protected:
      void initialize(Application&amp; self)
      {
          loadConfiguration(); // load default configuration files, if present
          ServerApplication::initialize(self);
      }
  
      void uninitialize()
      {
          ServerApplication::uninitialize();
      }
  
      void defineOptions(OptionSet&amp; options)
      {
          ServerApplication::defineOptions(options);
  
          options.addOption(
              Option("help", "h", "display help information on command line arguments")
              .required(false)
              .repeatable(false));
      }
  
      void handleOption(const std::string&amp; name, const std::string&amp; value)
      {
          ServerApplication::handleOption(name, value);
  
          if (name == "help")
              _helpRequested = true;
      }
  
      void displayHelp()
      {
          HelpFormatter helpFormatter(options());
          helpFormatter.setCommand(commandName());
          helpFormatter.setUsage("OPTIONS");
          helpFormatter.setHeader("A server application that serves the current date and time.");
          helpFormatter.format(std::cout);
      }
  
      int main(const std::vector&lt;std::string&gt;&amp; args)
      {
          if (_helpRequested)
          {
              displayHelp();
          }
          else
          {
              unsigned short port = (unsigned short)config().getInt("tcpport", 9911);
              std::cout &lt;&lt; "Using port " &lt;&lt; port &lt;&lt; std::endl;
              std::string format(config().getString("TimeServer.format", DateTimeFormat::ISO8601_FORMAT));
  
              ServerSocket svs(port);
              TCPServer srv(new TimeServerConnectionFactory(format), svs);
              srv.start();
              std::cout &lt;&lt; "Server started!\n";
              waitForTerminationRequest();
              srv.stop();
              std::cout &lt;&lt; "Server stopped!\n";
          }
          return Application::EXIT_OK;
      }
  
  private:
      bool _helpRequested;
  };
  
  
  int main(int argc, char** argv)
  {
      TimeServer app;
      UDPServer app2;
      app.run(argc, argv);
      app2.run(argc, argv);
  }
  </code></pre>
  
  <p>In the end of code I have <code>int main()</code> method where I'm trying to run 2 servers. However I get assertion violation here. There is a <a href="https://stackoverflow.com/questions/28410694/multiple-http-servers-with-poco-and-boost-c">similar question</a> on StackOverflow, however <code>boost</code> is used there while I'm using plain C++, so that solution is not relevant to me.</p>
  
  <p>How can I run simultaneously these 2 servers?</p>