首页 > 解决方案 > 服务器不从客户端读取

问题描述

程序要点:服务器创建 n 个客户端进程。在客户端,用户输入一个字符串,发送到服务器。在服务器上,对字符串进行如下处理:计算输入字符串中元音和数字出现的频率。然后将此信息发送给客户端。

两个程序(服务器和客户端)基于控制台版本(它们运行良好)。当我向服务器添加 QT 支持时,控制台客户端和 gui 服务器工作正常,但是当我向客户端添加 gui 时 - 它停止工作,我不知道该怎么办。我猜这个问题甚至不是命名管道,而是Qt(在客户端)方面的笨拙代码。客户端输出与服务器的连接已建立,服务器指示它无法从管道中读取字符串。服务器可以创建 n 个 gui 客户端,然后,当我尝试向客户端写入字符串时,客户端关闭。

客户端代码:

gui_pipe_client.h:

#pragma once

#include <LotsOfLibraries...>

class Window : public QWidget {

Q_OBJECT // этот макрос должен включаться в классы, которые объявляют свои собственные сигналы и слоты

private:
  HANDLE. m_hndlNP;

public:
  Window(QWidget *parent = 0, HANDLE m_hndlNP = NULL);

private slots:
  int OnSend();

private:

  QLabel *lbl;
  QLineEdit *textfield;
  QPushButton *button_send;
};

gui_pipe_client.cpp:

#include "gui_pipe_client.h"

#include <QGridLayout>
#include <thread> //!NEW!

using namespace std;

#define PIPE_TIMEOUT 5000
#define BUFSIZE 4096


Window::Window(QWidget *parent, HANDLE hndlNP)
    : QWidget(parent), m_hndlNP(hndlNP){


QLineEdit *textfield = new QLineEdit(this);
textfield->setPlaceholderText("Enter string...");



QPushButton *button_send = new QPushButton("Send", this);

QPushButton *button_quit = new QPushButton("Quit", this);

lbl = new QLabel("kjiqofj13fjio2;3fjl;3jiofj2l3;fij3;fioj423j;fj32jf;o3f", this);
lbl->setWordWrap(true);

    QGridLayout *grid = new QGridLayout(this);
    grid->addWidget(textfield, 0, 0);
    grid->addWidget(button_send, 1, 0);
    grid->addWidget(button_quit, 2, 0);
    grid->addWidget(lbl, 3, 0);

  setLayout(grid);

 connect(button_send, &QPushButton::clicked, this, &Window::OnSend); // привязка сигнала и слота
 connect(button_quit, &QPushButton::clicked, qApp, &QApplication::quit); // signal - slot
}

int Window::OnSend() {

button_send->setEnabled(false);



    char text[1024];

    

    const QByteArray stringData = textfield->text().toUtf8();
    //char text[100];
    text[qMin(1023,stringData.size())]='\0';
    std::copy(stringData.constBegin(),stringData.constBegin()+qMin(1023,stringData.size()),text);

    printf("%s", text);
    

    
    char answer[1024];
    DWORD read_bytes;
    DWORD written_bytes;

    if (WriteFile(m_hndlNP, text, 1024, &written_bytes, NULL)) {

        if (!ReadFile(m_hndlNP, answer, 1024, &read_bytes, NULL)) {
            //printf("ReadFile failed with %d.\n", GetLastError());
            system("pause"); // TEMPORARY
            //return EXIT_FAILURE;
        }
    }
    else {
        printf("WriteFile failed with %d.\n", GetLastError());
        system("pause"); // TEMPORARY
        //return EXIT_FAILURE;
    }
    //cout << "Writting and reading was successful\n";

    
    lbl->setText((QString) (answer));
    
    // ***CLOSING PIPE CONNECTION***
    CloseHandle(m_hndlNP);

    
    //QApplication::quit();
    }

主.cpp:

#include "gui_pipe_client.h"

using namespace std;

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

cout << "Client is launched\n";

    HANDLE hndlNP = CreateFile(
        TEXT("\\\\.\\pipe\\os_lab4_pipe"),
        GENERIC_READ | GENERIC_WRITE,
        NULL, // Prevents other processes from opening a file or device
        NULL, // cannot be inherited by any child processes
        OPEN_EXISTING,
        NULL, // no attributes
        NULL // no template
        );
    if (hndlNP == INVALID_HANDLE_VALUE) {
        printf("CreateFile error\n");
        cout << "CreateFile error\n";
        //return EXIT_FAILURE;
    }
    cout << "Pipe connection established\n";
    QApplication app(argc, argv);


    Window window(0, hndlNP);

    window.resize(600, 600);
    window.setWindowTitle("Pipe client");
    window.show();

    return app.exec();
}

标签: c++qtwinapiqt5named-pipes

解决方案


推荐阅读