Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9231351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:02:38+00:00 2026-06-18T06:02:38+00:00

I am starting a new project and at the same time have just discovered

  • 0

I am starting a new project and at the same time have just discovered Poco Library, which I find absolutely amazing. However I am a bit lost, as examples are not plenty.

I have a ServerApplication->TCPServer->ServerSocket + TCPServerConnectionFactory->TCPServerconnection approach as indicated by the examples. I am inheriting from the PocoNet classes as instructed. Right now I can run my server as a service, & receive incoming connections.

I want to take an event handling approach to the following: on a per-connection (or per client) basis, handle events such as data available to read on the client socket, error occurred on the client socket (disconnected or timeout), send data without error on client socket.

How do I go about doing it?
Is Poco/Foundation/Events what I am looking for, or is there some mechanism implemented in Poco::Net?

I have seen the Poco::Net::NetExpections but they do not appear to be thrown in my TCPServerConnection-derived class when a netcat connection closes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:02:39+00:00Added an answer on June 18, 2026 at 6:02 am

    What I ended up using is a different approach as TCPServer is a different beast altogether. Following the example posted here I ended up with a class inheriting from ServerApplication, and a class which becomes essentially the connection handler by a SocketReactor.

    Deamonizer header:

    class Daemon : public ServerApplication
    {
      public:
        Daemon();
        /// @Brief The main loop of the daemon, everything must take place here
        int main();
    };
    

    Deamonizer implementation:

    int Daemon::main()
    {
      // Server Socket
      ServerSocket svs(2222);
      // Reactor-Notifier
      SocketReactor reactor;
      Poco::Timespan timeout(2000000); // 2Sec
      reactor.setTimeout(timeout);
      // Server-Acceptor
      SocketAcceptor<ConnectionHandler> acceptor(svs, reactor);
      // Threaded Reactor
      Thread thread;
      thread.start(reactor);
      // Wait for CTRL+C
      waitForTerminationRequest();
      // Stop Reactor
      reactor.stop();
      thread.join();
      return Application::EXIT_OK;  
    }
    

    The handler class can be anything as long as it has a conforming Constructor (see Poco::Net documentation for this).
    In my case the header looks like this:

    class ConnectionHandler
    {
      public:
    
        /**
         * @Brief Constructor of the Connection Handler
         * @Note Each object is unique to an accepted connection
         * @Param SteamSocket is the socket accepting the connections
         * @See SocketAcceptor http://pocoproject.org/docs/Poco.Net.SocketAcceptor.html
         * @Param SocketReactor is the reacting engine (threaded) which creates notifications about the socket
         */
        ConnectionHandler(StreamSocket &, SocketReactor &);
    
        /**
         * @Brief Destructor
         */
        ~ConnectionHandler();
    
        /**
        * @Brief Event Handler when Socket becomes Readable, i.e: there is data waiting to be read
        */
        void onSocketReadable(const AutoPtr<ReadableNotification>& pNf);
    
        /**
        * @Brief Event Handler when Socket was written, i.e: confirmation of data sent away (not received by client)
        */
        void onSocketWritable(const AutoPtr<WritableNotification>& pNf);
    
        /**
        * @Brief Event Handler when Socket was shutdown on the remote/peer side
        */
        void onSocketShutdown(const AutoPtr<ShutdownNotification>& pNf);
    
        /**
        * @Brief Event Handler when Socket throws an error
        */
        void onSocketError(const AutoPtr<ErrorNotification>& pNf);
    
        /**
        * @Brief Event Handler when Socket times-out
        */
        void onSocketTimeout(const AutoPtr<TimeoutNotification>& pNf);
    
      private:
    
        /**
         * @Brief Read bytes from the socket, depending on available bytes on socket
         */
        void readBytes();
    
        /**
         * @Brief Send message to the socket
         * @Param std::string is the message (null terminated)
         */
        void sendMessage(std::string);
    
        /// Stream Socket
        StreamSocket _socket;
    
        /// Socket Reactor-Notifier
        SocketReactor& _reactor;
    
        /// Received Data Buffer
        std::vector<char *> in_buffer;
    };
    

    How you implement the handler is up to you, provided the only thing that you need to do is register the class methods which handle events like so:

      _reactor.addEventHandler(_socket,NObserver<ConnectionHandler, ReadableNotification>(*this, &ConnectionHandler::onSocketReadable));
      _reactor.addEventHandler(_socket,NObserver<ConnectionHandler, ShutdownNotification>(*this, &ConnectionHandler::onSocketShutdown));
      _reactor.addEventHandler(_socket,NObserver<ConnectionHandler, ErrorNotification>(*this, &ConnectionHandler::onSocketError));
      _reactor.addEventHandler(_socket,NObserver<ConnectionHandler, TimeoutNotification>(*this, &ConnectionHandler::onSocketTimeout));
    

    All in all, two classes, a few lines of code, simple and clean. Absolutely starting to love Poco library! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm starting a new project which in simple terms will have a UI layer
I am starting a new project for a client today. I have done some
I'm starting a new project and i've recently found castle project activerecord, which seems
I am starting a new project in Asp.net MVC 2. I have been mostly
I'm starting a new Zend Framework project in which I will collaborate with a
We are starting a new project based on EJB 3.0. I have a spring
I'm starting new project. The client interface is based on GWT (and GXT) I
How to improve our productivity when starting new project using Delphi? For me, I
Starting a new project using EJB 3 / JPA, mainly stateless session beans and
we are starting a new project and I would like to know if V5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.