C + + Poco library Chinese Programming Reference Guide (10) How to use the TCPServer framework?

Source: Internet
Author: User

1 TCPServer Framework Overview

The POCO library provides a tcpserver framework to build a custom TCP server. TCPServer maintains a connection queue, a connection thread pool. The connection thread is used to process the connection, and the connection thread continuously takes the connection from the connection queue and processes it as soon as it is idle. Once the connection thread takes a connection from the connection queue, a Tcpserverconnection connection object is created and the start () method of the object is invoked until the start () method returns, and the connection object is deleted.

The number of connection threads is dynamic, depending on the number of connections queued in the connection queue. Of course, you can set the maximum capacity of the connection queue when you use it, in order to prevent the tragedy that the connection queue overflows when there is too much connection on the high concurrent application server. When the connection queue is full and a new connection arrives, the new connection is immediately closed silently.

Now let's conclude by having a running TCP service application (named Poechanttcpserver) and a lot of TCP connections (named Poechanttcpconnection). And here we use the Factory model (exactly what Tcpserverconnectionfactory wants us to use), so there's a poechanttcpconnectionfactory.

2 Light said not practice fake

2.1 Create a Poechanttcpserver

Perhaps you are not familiar with the application in POCO, it doesn't matter, this does not affect the narrative of this article. The following first creates a serverapplication as follows:

PoechantTCPServer.h   

#ifndef poechant_tcp_server   
#define Poechant_tcp_server   

#include "poco/util/ ServerApplication.h "   
#include" poco/util/application.h "   

using Poco::util::serverapplication;   
Using Poco::util::application;

Class Poechanttcpserver:public serverapplication   
{public   
:   
    poechanttcpserver () {}
    ~ Poechanttcpserver () {}
protected:
    void Initialize (application& self);   
    void Uninitialize ();   
    int main (const std::vector<std::string>& args)   
};   

#endif

This invokes the initialize and then calls main when the poechanttcpserver is invoked, and the uninitialize is invoked after main finishes. The implementation is simple:

PoechantTCPServer.cpp   

#include "PoechantTCPServer.h"   

void Poechanttcpserver::initialize (Application & self)   
{   
    serverapplication::loadconfiguration ();   
    Serverapplication::initialize (self);   
}   

void Poechanttcpserver::uninitialize ()   
{   
    serverapplication::uninitialize ();   
}   

int Poechanttcpserver::main (const std::vector<std::string>& args)   
{   
    //This I finally said return   

    APPLICATION::EXIT_OK;   
}

2.2 Poechanttcpconnection

The definition of a connection class is simple, and the constructor passes in a streamsocket and other parameters you need.

PoechantTCPConnection.h   

#ifndef poechant_tcp_connection_h   
#define POECHANT_TCP_CONNECTION_H   

# Include "Poco/net/tcpserverconnection.h"   
#include "poco/net/streamsocket.h"   
#include <string>   

Class Poechanttcpconnection:public tcpserverconnection   
{public   
:   
    poechanttcpconnection (const streamsocket& s,   
        const std::string& arg1,   
        int arg2,   
        double arg3);   
       
    void run ();   
Private:   
       
    std::string _arg1;   
    int _arg2;   
    Double _arg3;   
};   

#endif

Implemented as follows:

//PoechantTCPConnection.cpp #include "PoechantTCPConnection.h" #include "poco/util/application" #include "Poco/timestamp.h" #include "poco/exception.h" #include "poco/datetimeformatter.h" poechanttcpconnection (const S treamsocket& s, const std::string& arg1, int arg2, double arg3): Tcpserverconnection (s), _arg1 (arg1), _arg2   
    (ARG2), _arg3 (ARG3) {} void Run () {application& app = Application::instance ();   
    Log output the address (IP and port) of the TCP user connected App.logger (). Information ("Request from" + This->socket (). Peeraddress (). toString ());   
        try {//Send data to the client, as an example of sending a string representing the time Timestamp now;   
        std::string DT (Datetimeformatter::format (now, _format));   
        Dt.append ("\ r \ n");   
    Socket (). Sendbytes (Dt.data (), (int) dt.length ());   
    catch (poco::exception& e) {app.logger (). log (e); }   
}

2.3 poechanttcpconnectionfactory

The factory model doesn't have to say much, the name Bluff, in fact very very simple (accurate design pattern most names are bluffing, but most of them are very useful, design pattern itself is not a cow B, can abstract design patterns as we now think very simple these patterns of those who are very bull B). Specifically as follows:

PoechantTCPConnectionFactory.h   

#ifndef poechant_tcp_connection_factory_h   
#define Poechant_tcp_ Connection_factory_h   

#include "poco/net/tcpserverconnectionfactory.h"   
#include "poco/net/ TCPServerConnection.h "   
#include" poco/net/streamsocket.h "   
#include <string>   

class Poechanttcpconnectionfactory:public tcpserverconnectionfactory   
{public   
    :   
    Poechanttcpconnectionfactory (const std::string arg1, int arg2, double arg3)   
        : _arg1 (arg1), _arg2 (arg2), _arg3 (arg3   
    {   
    }   

    tcpserverconnection* createconnection (const streamsocket& socket) {return   
        new Poechanttcpconnection (socket, arg1, arg2, ARG3);   
    }   

Private:   
    std::string arg1;   
    int arg2;   
    Double arg3;   
};   

#endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.