Overview
Features
Download
Documentation
Community
Add-Ons & Services

Server Threading

Please post support and help requests here.

Server Threading

Postby mad93 » 04 Nov 2009, 22:48

Hi, I'm a new user of Poco and a relatively new user of C++, so please, be patient with me ;)

I'm trying to write a server (i'm thinking to use JSON to comunicate with clients), and i'm already done a server with c (for other pourposes), with threading, semaphores, shared memory, etc.. But I'm a bit confused about how Poco works and how to implement the server with Poco.

Currently I've the server listening for incoming connections (throught logger it reports when a client connects). I'm a bit confused with the several alternatives to do threading with Poco, and i don't know what is the most suitable for my needs. I need that the parent allways listen for incoming connections and the child deals with the client.

So i listen with a ServerSocket and wait with the method acceptConnection:

Code: Select all
while(1)
                {
                        string info;
                        ss = sk.acceptConnection();
                        info = "entra un client amb ip: " + ss.address().host().toString();
                        logger().information(info);
                        //Hem de cridar ara al fill
                        Client client(ss);
                }


And Client uses Activity to do threading.
Client.h
Code: Select all
#include <iostream>
#include <Poco/Activity.h>
#include <Poco/Net/StreamSocket.h>


using namespace std;

using Poco::Activity;
using Poco::Net::StreamSocket;

class Client
{
public:
    Client(StreamSocket _ss);
protected:
    void runActivity();
private:
    Activity<Client> _activity;
    StreamSocket ss;
};


Client.cpp
Code: Select all
#include <iostream>
#include <Poco/Activity.h>
#include <Poco/Util/Application.h>
#include <Poco/Net/StreamSocket.h>
#include "Client.h"

using namespace std;

using Poco::Activity;
using Poco::Net::StreamSocket;
using Poco::Util::Application;

Client::Client(StreamSocket _ss) : _activity(this, &Client::runActivity)
{
        ss=_ss;
        _activity.start();
}

void Client::runActivity()
{
        while (!_activity.isStopped())
        {
                cout << "arg" << endl;
                ss.sendBytes("hola que tal\0", 5);
                //ss.flush();
        }
        cout << "no entro" << endl;
        while(1) ss.sendBytes("hola que tal\0", 5);
}


As you can see is almost the example of an Activity that you find in the documentation.

When i do the _activity.start() it executes Client::runActivity() but _activity.isStopped() returns !(true).

Also i don't understand the _activity.isStopped() (I understand it as "The child is asking itself if its running" what doesn't make sense, so I'm wrong for sure ^^u).

If somebody can explain to me my conceptual mistakes and also tell me if it's a good aproach to a ServerSocket implementation I'll be thankfull.
mad93
 
Posts: 6
Joined: 04 Nov 2009, 22:13

Re: Server Threading

Postby alex » 05 Nov 2009, 12:33

mad93 wrote:When i do the _activity.start() it executes Client::runActivity() but _activity.isStopped() returns !(true).

Which makes sense, because you have started it in the constructor, so it is not stopped.
mad93 wrote:Also i don't understand the _activity.isStopped() (I understand it as "The child is asking itself if its running" what doesn't make sense

It depends how you look at it. Think of it as the child asking whether someone else has stopped it.
mad93 wrote:If somebody can explain to me my conceptual mistakes

You have to switch to multi-threaded mode of thinking. Look at the example code:
Code: Select all
ActivityExample example;
example.start();
Thread::sleep(2000);
example.stop();

That code runs in main thread and controls the activity. The activity object runs in its own thread, so it makes sense when it wants to check whether the main thread has stopped it.
mad93 wrote:tell me if it's a good aproach to a ServerSocket implementation I'll be thankfull.

Look at examples: TimeServer for threaded approach and EchoServer for reactor-based one.
alex
 
Posts: 1158
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Server Threading

Postby mad93 » 06 Nov 2009, 01:04

Thank you so much for your examples and explanation. I've it now working as a TCPServer with TCPServerFactory :)
mad93
 
Posts: 6
Joined: 04 Nov 2009, 22:13


Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests