• <small id='CmXfD'></small><noframes id='CmXfD'>

      <bdo id='CmXfD'></bdo><ul id='CmXfD'></ul>
  • <tfoot id='CmXfD'></tfoot>

  • <legend id='CmXfD'><style id='CmXfD'><dir id='CmXfD'><q id='CmXfD'></q></dir></style></legend>

    <i id='CmXfD'><tr id='CmXfD'><dt id='CmXfD'><q id='CmXfD'><span id='CmXfD'><b id='CmXfD'><form id='CmXfD'><ins id='CmXfD'></ins><ul id='CmXfD'></ul><sub id='CmXfD'></sub></form><legend id='CmXfD'></legend><bdo id='CmXfD'><pre id='CmXfD'><center id='CmXfD'></center></pre></bdo></b><th id='CmXfD'></th></span></q></dt></tr></i><div id='CmXfD'><tfoot id='CmXfD'></tfoot><dl id='CmXfD'><fieldset id='CmXfD'></fieldset></dl></div>

        如何以几秒的间隔多次使用 QTcpSocket 实例?

        How to use QTcpSocket instance multiple times with couple second intervals?(如何以几秒的间隔多次使用 QTcpSocket 实例?)
        1. <i id='xM0Ux'><tr id='xM0Ux'><dt id='xM0Ux'><q id='xM0Ux'><span id='xM0Ux'><b id='xM0Ux'><form id='xM0Ux'><ins id='xM0Ux'></ins><ul id='xM0Ux'></ul><sub id='xM0Ux'></sub></form><legend id='xM0Ux'></legend><bdo id='xM0Ux'><pre id='xM0Ux'><center id='xM0Ux'></center></pre></bdo></b><th id='xM0Ux'></th></span></q></dt></tr></i><div id='xM0Ux'><tfoot id='xM0Ux'></tfoot><dl id='xM0Ux'><fieldset id='xM0Ux'></fieldset></dl></div>

              <tfoot id='xM0Ux'></tfoot>

              • <bdo id='xM0Ux'></bdo><ul id='xM0Ux'></ul>
                • <legend id='xM0Ux'><style id='xM0Ux'><dir id='xM0Ux'><q id='xM0Ux'></q></dir></style></legend>
                    <tbody id='xM0Ux'></tbody>

                  <small id='xM0Ux'></small><noframes id='xM0Ux'>

                  本文介绍了如何以几秒的间隔多次使用 QTcpSocket 实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我必须用 QTcpSocket 多次重复相同的请求,间隔几秒钟.简单的任务,但我不能让它只处理一个对象实例.如何使用相同的 QTcpSocket 实例再次连接到服务器?我已经尝试了许多不同组合的 reset()、resume()、flush()、open(),但它什么也没做.我第二次无法 connectToHost()...

                  I have to repeat the same request with QTcpSocket multiple times with a couple seconds intervals. Easy task but I can't get it to work with only one instance of object. How to connect to server again using the same instance of QTcpSocket? I've tried reset(), resume(), flush(), open() in many diffrent combinations and it has done nothing. I'm unable to connectToHost() for second time...

                  推荐答案

                  你要做的就是connectToHost(),使用连接,然后disconnectFromHost().而已.不多不少.

                  All you have to do is to connectToHost(), use the connection, then disconnectFromHost(). That's it. Nothing more, nothing less.

                  下面的例子说明了客户端和服务器套接字实例的重用.服务器实例保存在一个仅在必要时增长的池中.它是为 Qt 5 编写的,使用 C++11.

                  The below example illustrates the reuse of both client and server socket instances. The server instances are kept in a pool that only grows when necessary. It's written for Qt 5 and uses C++11.

                  // https://github.com/KubaO/stackoverflown/tree/master/questions/multisocket-22726075
                  #include <QtNetwork>
                  
                  class EchoServer : public QTcpServer
                  {
                     QStack<QTcpSocket*> m_pool;
                     void incomingConnection(qintptr descr) Q_DECL_OVERRIDE {
                        if (m_pool.isEmpty()) {
                           auto s = new QTcpSocket(this);
                           QObject::connect(s, &QTcpSocket::readyRead, s, [s]{
                              s->write(s->readAll());
                           });
                           QObject::connect(s, &QTcpSocket::disconnected, this, [this, s]{
                              m_pool.push(s);
                           });
                           m_pool.push(s);
                        }
                        m_pool.pop()->setSocketDescriptor(descr, QTcpSocket::ConnectedState);
                     }
                  public:
                     ~EchoServer() { qDebug() << "pool size:" << m_pool.size(); }
                  };
                  
                  void setupEchoClient(QTcpSocket & sock)
                  {
                     static const char kByteCount[] = "byteCount";
                     QObject::connect(&sock, &QTcpSocket::connected, [&sock]{
                        auto byteCount = 64 + qrand() % 65536;
                        sock.setProperty(kByteCount, byteCount);
                        sock.write(QByteArray(byteCount, 'x2A'));
                     });
                     QObject::connect(&sock, &QTcpSocket::readyRead, [&sock]{
                        auto byteCount = sock.property(kByteCount).toInt();
                        if (byteCount) {
                           auto read = sock.read(sock.bytesAvailable()).size();
                           byteCount -= read;
                        }
                        if (byteCount <= 0) sock.disconnectFromHost();
                        sock.setProperty(kByteCount, byteCount);
                     });
                  }
                  
                  int main(int argc, char *argv[])
                  {
                     QCoreApplication a(argc, argv);
                     QHostAddress addr("127.0.0.1");
                     quint16 port = 5050;
                  
                     EchoServer server;
                     if (! server.listen(addr, port)) qFatal("can't listen");
                  
                     QTcpSocket clientSocket;
                     setupEchoClient(clientSocket);
                  
                     auto connectsLeft = 20;
                     auto connector = [&clientSocket, &addr, port, &connectsLeft]{
                        if (connectsLeft--) {
                           qDebug() << "connecting" << connectsLeft;
                           clientSocket.connectToHost(addr, port);
                        } else
                           qApp->quit();
                     };
                     // reconnect upon disconnection
                     QObject::connect(&clientSocket, &QTcpSocket::disconnected, connector);
                     // initiate first connection
                     connector();
                  
                     return a.exec();
                  }
                  

                  这篇关于如何以几秒的间隔多次使用 QTcpSocket 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                    <small id='EejR5'></small><noframes id='EejR5'>

                      <i id='EejR5'><tr id='EejR5'><dt id='EejR5'><q id='EejR5'><span id='EejR5'><b id='EejR5'><form id='EejR5'><ins id='EejR5'></ins><ul id='EejR5'></ul><sub id='EejR5'></sub></form><legend id='EejR5'></legend><bdo id='EejR5'><pre id='EejR5'><center id='EejR5'></center></pre></bdo></b><th id='EejR5'></th></span></q></dt></tr></i><div id='EejR5'><tfoot id='EejR5'></tfoot><dl id='EejR5'><fieldset id='EejR5'></fieldset></dl></div>
                      • <bdo id='EejR5'></bdo><ul id='EejR5'></ul>
                      • <legend id='EejR5'><style id='EejR5'><dir id='EejR5'><q id='EejR5'></q></dir></style></legend>
                            <tbody id='EejR5'></tbody>

                          • <tfoot id='EejR5'></tfoot>