如何使用QTcpSocket实例多次与几秒的间隔? [英] How to use QTcpSocket instance multiple times with couple second intervals?

查看:188
本文介绍了如何使用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实例多次与几秒的间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆