Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Windows 11 Named Pipe QLocalServer
Forum Updated to NodeBB v4.3 + New Features

Windows 11 Named Pipe QLocalServer

Scheduled Pinned Locked Moved Solved General and Desktop
1 Posts 1 Posters 284 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    king558
    wrote on last edited by king558
    #1

    First of all the server code

    server.cpp

    #include "server.h"
    
    #include <QtWidgets>
    #include <QtNetwork>
    
    Server::Server(QWidget *parent)
        : QDialog(parent)
    {
        setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    
        server = new QLocalServer(this);
        if (!server->listen("fortune")) {
            QMessageBox::critical(this, tr("Local Fortune Server"),
                                  tr("Unable to start the server: %1.")
                                  .arg(server->errorString()));
            close();
            return;
        }
    
        QLabel *statusLabel = new QLabel;
        statusLabel->setWordWrap(true);
        statusLabel->setText(tr("The server is running.\n"
                                "Run the Local Fortune Client example now."));
    
        fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
                 << tr("You've got to think about tomorrow.")
                 << tr("You will be surprised by a loud noise.")
                 << tr("You will feel hungry again in another hour.")
                 << tr("You might have mail.")
                 << tr("You cannot kill time without injuring eternity.")
                 << tr("Computers are not intelligent. They only think they are.");
    
        QPushButton *quitButton = new QPushButton(tr("Quit"));
        quitButton->setAutoDefault(false);
        connect(quitButton, &QPushButton::clicked, this, &Server::close);
        connect(server, &QLocalServer::newConnection, this, &Server::connected);
    
        QHBoxLayout *buttonLayout = new QHBoxLayout;
        buttonLayout->addStretch(1);
        buttonLayout->addWidget(quitButton);
        buttonLayout->addStretch(1);
    
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
        mainLayout->addWidget(statusLabel);
        mainLayout->addLayout(buttonLayout);
    
        setWindowTitle(QGuiApplication::applicationDisplayName());
    }
    
    void Server::connected() {
        auto clientConnection = server->nextPendingConnection();
        QObject::connect( clientConnection, &QLocalSocket::readyRead, this, &Server::dataReady );
        QObject::connect( clientConnection, &QLocalSocket::disconnected, clientConnection, &QLocalSocket::deleteLater );
        vclient.push_back( clientConnection );
    }
    
    void Server::dataReady() {
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_5_10);
        const int fortuneIndex = QRandomGenerator::global()->bounded(0, fortunes.size());
        const QString &message = fortunes.at(fortuneIndex);
        out << quint32(message.size());
        out << message;
    
        int index = 0;
        for ( auto &client : vclient ) {
            QString outBuffer = QString::number( index++ ) + " : " + block;
            client->write(outBuffer.toLatin1());
            client->flush();
            client->disconnectFromServer();
        }
    }
    

    client use Winapi,

    ConnectNamedPipe - connect to Pipe server
    PeekNamedPipe - check if data is available
    ReadFile - Read the data

    Currently I get the Problem, that sometimes all clients got the same message with same index ID, it was intended that every client received data with different index indicated by index number.

    I also recherche that there are option for Single and Multiple Instance. It seems that currently QLocalServer handles it like Option 1 How to setup QLocalServer with Multiple Instance like Option 2?

    9671f345-e69d-4309-bec1-d66a8d519173-image.png

    1 Reply Last reply
    0
    • K king558 has marked this topic as solved on

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved