【问题标题】:C++ Multi-Client TCP Server with QList带有 QList 的 C++ 多客户端 TCP 服务器
【发布时间】:2016-03-28 15:54:03
【问题描述】:

必要信息:

  QList<QTcpSocket*> list;
  QTcpServer* server;
  QTcpSocket* socket;

在 Qt 中我建立了一个 TCP-Server(QTcpServer)!我与所有连接的客户都有一个 QList,我想亲自阅读每个客户的传入数据。因此,如果 QTcpServer 获得新连接,我会这样处理:

void Server::newConnection()
{
   qDebug() << "New Connection";
   list.append(server->nextPendingConnection());
   connect(list.last(),SIGNAL(readyRead()),this,SLOT(readyRead()));
}

我怎样才能获得正确的客户端(从我的 QList 中),它在我的 SLOT @ 中发送 SIGNAL readyRead() 987654324@?

void Server::readyRead(){
       //??
}

欢迎任何帮助!

【问题讨论】:

  • 答案不属于问题。可以回答您自己的问题 - 在答案中。

标签: c++ qt tcp qlist


【解决方案1】:

你试过 QObject::sender() 吗?它应该返回实际发送信号的 QObject 的实例。希望这会有所帮助。

【讨论】:

  • 是的,谢谢! sender() 是正确的做法!如果有人想对我的代码做点什么,那就在上面!
  • 如何使用 sender() ?我试过`QTcpSocket* socket = QObject::sender();`
【解决方案2】:

解决办法:

void Server::readyRead(){

    QByteArray buffer;
    QTcpSocket* readSocket = qobject_cast<QTcpSocket*>(sender());
    buffer = readSocket->readAll();

    QString mytext = QString::fromStdString(buffer);
    qDebug() << mytext;
}

【讨论】:

  • 文本 = 缓冲区?
【解决方案3】:

这可以通过QSignalMapper 解决。这是(未完全测试)代码:

---------------------- main.cpp ------------------

#include "rootwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    RootWindow w;
    w.show();

    return a.exec();
}

---------- rootwindow.h ---------- --

#ifndef ROOTWINDOW_H
#define ROOTWINDOW_H

#include <QMainWindow>
#include <QtDebug>
#include <QLocalServer>
#include <QLocalSocket>
#include <QSignalMapper>
#include <QList>

class RootWindow : public QMainWindow
{
    Q_OBJECT

private:

    QLocalServer *server;
    QLocalSocket *socket;

    QList<QLocalSocket*> *list;

    QSignalMapper *mapper;

public:

    RootWindow(QWidget *parent = 0);
    ~RootWindow();

private slots:

    void slotNewConnection();
    void slotReadyRead(int index);
};

#endif // ROOTWINDOW_H

------------ rootwindow.cpp -------------------- -----

#include "rootwindow.h"

RootWindow::RootWindow(QWidget *parent): QMainWindow(parent)
{
    server = new QLocalServer;
    list = new QList<QLocalSocket*>;

    mapper = new QSignalMapper(this);

    connect(server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
    connect(mapper, SIGNAL(mapped(int)), this, SLOT(slotReadyRead(int)));

    server->listen("TestServer");
}

RootWindow::~RootWindow()
{
    delete list;
}

void RootWindow::slotNewConnection()
{
    qWarning() << "newConnection";
    list->append(server->nextPendingConnection());

    //here you map each client to its number in the list
    mapper->setMapping(list->last(), list->length()-1);

    //here we say, that when ever a client from the QList sends readyRead() the mapper should be used
    //with the property (list->length()-1) defined in the line above
    connect(list->last(), SIGNAL(readyRead()), mapper, SLOT(map()));
}

void RootWindow::slotReadyRead(int index)
{
    qWarning() << "Client " << index << " has written: " << list->at(index)->readAll();
}

这基本上是你的代码,我只在相关行添加了QSignalMapper 和一些 cmets。

【讨论】:

    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多