【问题标题】:Poco TCPServer does not start outside mainPoco TCPServer 不在 main 外部启动
【发布时间】:2017-12-10 13:39:17
【问题描述】:

当我在 main 中初始化 TCPServer 时它可以工作,当我尝试使用 startServer() 函数启动它时它不起作用,我的意思是我无法与 putty 建立连接。

我在这里做错了什么?

感谢您的帮助。

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();
}

【问题讨论】:

  • 为什么实际上需要在main()之外初始化它?
  • 没有必要在 main 之外初始化它。但我想将它移动到它自己的 cpp 文件中。我想了解在 main 之外进行初始化时的不同之处。
  • 无法保证在main() 之外执行的订单初始化序列及其执行顺序。这可能是您观察到的行为的原因。
  • 如果我理解正确的话,tcp 服务器在它自己的线程中运行。会不会是在 main 之外进行 init 时线程停止了?
  • 我怀疑线程是否会在进程的主线程初始化之前正确初始化。

标签: c++ poco tcpserver


【解决方案1】:

startServer 函数结束时srv 对象被删除。 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,因此线程完成工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2013-10-28
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多