2017-12-10 8 views
0

メインサーバー内でTCPServerの初期化を行うと、startServer()関数で起動しようとすると動作しません。パテ。Poco TCPServerがメイン以外で起動しない

私はここで間違っていますか?

ありがとうございました。 srvオブジェクトが削除されるstartServer関数の終わりに

class EchoConnection : public TCPServerConnection { 
public: 
EchoConnection(const StreamSocket& s) 
    : TCPServerConnection(s) {} 

void reply(char buffer[]) 
{ 
    bzero(buffer, 256); 
    std::string myWord = "myWord\n\r"; 
    strcpy(buffer, myWord.c_str()); 
} 

void run() { 
    StreamSocket& ss = socket(); 
    try { 
     char buffer[256]; 
     int n = ss.receiveBytes(buffer, sizeof(buffer)); 
     while (n > 0) { 
      reply(buffer); 
      ss.sendBytes(buffer, sizeof(buffer)); 
      n = ss.receiveBytes(buffer, sizeof(buffer)); 
     } 
    } 
    catch (Poco::Exception& exc) 
    { std::cerr << "EchoConnection: " << exc.displayText() << std::endl; } 
} 
}; 

void startServer() 
{ 
    Poco::Net::TCPServer srv(new Poco::Net::TCPServerConnectionFactoryImpl<EchoConnection>, 8089); 
    srv.start(); 

    SocketAddress sa("localhost", srv.socket().address().port()); 
    StreamSocket ss(sa); 
    std::string data("hello, world"); 
    ss.sendBytes(data.data(), (int)data.size()); 
    char buffer[256] = { 0 }; 
    int n = ss.receiveBytes(buffer, sizeof(buffer)); 
    std::cout << std::string(buffer, n) << std::endl; 
} 

int main(int argc, char** argv) { 

    Poco::Net::TCPServer srv(new Poco::Net::TCPServerConnectionFactoryImpl<EchoConnection>, 8089); 
    srv.start(); 

    SocketAddress sa("localhost", srv.socket().address().port()); 
    StreamSocket ss(sa); 
    std::string data("hello, world"); 
    ss.sendBytes(data.data(), (int)data.size()); 
    char buffer[256] = { 0 }; 
    int n = ss.receiveBytes(buffer, sizeof(buffer)); 
    std::cout << std::string(buffer, n) << std::endl; 

    // startServer(8089); 

    ModuleData modData; 
    modData.ModuleNumber = 1; 
    modData.ModuleTypeId = 1; 

    string test = modData.serialize(); 

    ifstream dataFile; 
    dataFile.open("ModuleData.dat"); 
    if (dataFile.is_open()) 
    { 
     string line; 
     while (getline(dataFile, line)) 
     { 
      cout << line << std::endl; 
     } 
     dataFile.close(); 
    } 

    while (1) 
    { 

    } 

    srv.stop(); 
} 
+0

実際に 'main()'の外側で初期化する必要があるのはなぜですか? – user0042

+0

main以外で初期化する必要はありません。しかし、私はそれを独自のcppファイルに移動したいと思います。私は初期化がメインの外で行われるときにどこが異なるかを理解することができます。 – Helmut

+0

初期化シーケンスが 'main()'の外で実行され、実行される順序についての保証はありません。それはあなたが観察する行動の理由かもしれません。 – user0042

答えて

0

TCPServerは独自のスレッドを使用しますが、TCPServerのデストラクタで作業を終了します。 〜TCPServerの

TCPServer::~TCPServer() { 
try { 
    stop(); 
    _pDispatcher->release(); 
... 
} 

stopメソッドの実装

void TCPServer::stop() { 
    if (!_stopped) 
    { 
     _stopped = true; // !! 
     _thread.join();  // waiting for thread 
     _pDispatcher->stop(); 
    } 
} 

を参照の実装で

ルックスレッド関数の本体がrun方法である

void TCPServer::run() 
{ 
    while (!_stopped)  // this flag was set by stop method 
    { 
    ... // body of thread function 
    } 
} 

stop方法は_stoppedを設定し、 trueの場合、スレッドは作業を終了します。

関連する問題